-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efd1f17
commit c11693a
Showing
12 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## mica-nats(高性能、轻量级消息队列) | ||
|
||
nats 封装,更改方便 Spring boot 下使用。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
dependencies { | ||
api "io.nats:jnats:2.16.13" | ||
implementation "org.springframework.boot:spring-boot-starter" | ||
compileOnly "org.springframework.cloud:spring-cloud-context" | ||
compileOnly "net.dreamlu:mica-auto:${micaAutoVersion}" | ||
annotationProcessor "net.dreamlu:mica-auto:${micaAutoVersion}" | ||
} |
32 changes: 32 additions & 0 deletions
32
mica-nats/src/main/java/net/dreamlu/mica/nats/annotation/NatsListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & www.dreamlu.net). | ||
* <p> | ||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.gnu.org/licenses/lgpl.html | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.dreamlu.mica.nats.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* nats 监听器 | ||
* | ||
* @author L.cm | ||
*/ | ||
@Documented | ||
@Inherited | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface NatsListener { | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
mica-nats/src/main/java/net/dreamlu/mica/nats/annotation/NatsStreamListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & www.dreamlu.net). | ||
* <p> | ||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.gnu.org/licenses/lgpl.html | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.dreamlu.mica.nats.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* nats stream 监听器注解 | ||
* | ||
* @author L.cm | ||
*/ | ||
@Documented | ||
@Inherited | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface NatsStreamListener { | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
mica-nats/src/main/java/net/dreamlu/mica/nats/config/NatsConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & www.dreamlu.net). | ||
* <p> | ||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.gnu.org/licenses/lgpl.html | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.dreamlu.mica.nats.config; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
|
||
/** | ||
* nats 配置 | ||
* | ||
* @author L.cm | ||
*/ | ||
@AutoConfiguration | ||
@EnableConfigurationProperties | ||
public class NatsConfiguration { | ||
} |
36 changes: 36 additions & 0 deletions
36
mica-nats/src/main/java/net/dreamlu/mica/nats/config/NatsProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & www.dreamlu.net). | ||
* <p> | ||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.gnu.org/licenses/lgpl.html | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.dreamlu.mica.nats.config; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.cloud.context.config.annotation.RefreshScope; | ||
|
||
/** | ||
* nats 配置 | ||
* | ||
* @author L.cm | ||
*/ | ||
@Getter | ||
@Setter | ||
@RefreshScope | ||
@ConfigurationProperties(NatsProperties.PREFIX) | ||
public class NatsProperties { | ||
public static final String PREFIX = "nats"; | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
mica-nats/src/main/java/net/dreamlu/mica/nats/config/NatsStreamConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & www.dreamlu.net). | ||
* <p> | ||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.gnu.org/licenses/lgpl.html | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.dreamlu.mica.nats.config; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
|
||
/** | ||
* nats stream 配置 | ||
* | ||
* @author L.cm | ||
*/ | ||
@AutoConfiguration | ||
public class NatsStreamConfiguration { | ||
} |
25 changes: 25 additions & 0 deletions
25
mica-nats/src/main/java/net/dreamlu/mica/nats/core/NatsStreamTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & www.dreamlu.net). | ||
* <p> | ||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.gnu.org/licenses/lgpl.html | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.dreamlu.mica.nats.core; | ||
|
||
/** | ||
* nats stream Template | ||
* | ||
* @author L.cm | ||
*/ | ||
public interface NatsStreamTemplate { | ||
} |
25 changes: 25 additions & 0 deletions
25
mica-nats/src/main/java/net/dreamlu/mica/nats/core/NatsTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & www.dreamlu.net). | ||
* <p> | ||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.gnu.org/licenses/lgpl.html | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.dreamlu.mica.nats.core; | ||
|
||
/** | ||
* nats Template | ||
* | ||
* @author L.cm | ||
*/ | ||
public interface NatsTemplate { | ||
} |
36 changes: 36 additions & 0 deletions
36
mica-nats/src/test/java/net/dreamlu/mica/nats/NatsExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package net.dreamlu.mica.nats; | ||
|
||
import io.nats.client.*; | ||
|
||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
/** | ||
* nats 测试 | ||
*/ | ||
public class NatsExample { | ||
|
||
public static void main(String[] args) { | ||
Options options = new Options.Builder() | ||
.server(Options.DEFAULT_URL) | ||
.build(); | ||
try { | ||
Connection nc = Nats.connect(options); | ||
new Timer().schedule(new TimerTask() { | ||
@Override | ||
public void run() { | ||
nc.publish("subject", "Hello, NATS!".getBytes()); | ||
} | ||
}, 3000, 3000); | ||
Dispatcher dispatcher = nc.createDispatcher(msg -> { | ||
byte[] bytes = msg.getData(); | ||
System.out.println(new String(bytes)); | ||
}); | ||
dispatcher.subscribe("subject"); | ||
Thread.sleep(10000L); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
mica-nats/src/test/java/net/dreamlu/mica/nats/PubWildcardSubWildcard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package net.dreamlu.mica.nats; | ||
|
||
import io.nats.client.*; | ||
import io.nats.client.api.PublishAck; | ||
import io.nats.client.api.StorageType; | ||
import io.nats.client.api.StreamConfiguration; | ||
import io.nats.client.api.StreamInfo; | ||
import io.nats.client.impl.NatsMessage; | ||
import io.nats.client.support.JsonUtils; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class PubWildcardSubWildcard { | ||
|
||
private static final String defaultStream = "pubsubwildcardasync-stream"; | ||
private static final String defaultSubjectWildcard = "audit.us.*"; | ||
private static final String defaultSubjectSpecific = "audit.us.east"; | ||
private static final String defaultMessage = "Audit User"; | ||
private static final int defaultMessageCount = 2; | ||
private static final String defaultServer = "nats://localhost:4222"; | ||
|
||
public static void main(String[] args) { | ||
System.out.printf("\nPublishing to %s. Server is %s\n\n", defaultSubjectWildcard, defaultServer); | ||
try (Connection nc = Nats.connect(defaultServer)) { | ||
JetStreamManagement jsm = nc.jetStreamManagement(); | ||
|
||
StreamConfiguration sc = StreamConfiguration.builder() | ||
.name(defaultStream) | ||
.storageType(StorageType.Memory) | ||
.subjects(defaultSubjectWildcard) | ||
.build(); | ||
|
||
StreamInfo streamInfo = jsm.addStream(sc); | ||
JsonUtils.printFormatted(streamInfo); | ||
|
||
JetStream js = nc.jetStream(); | ||
|
||
List<CompletableFuture<PublishAck>> futures = new ArrayList<>(); | ||
int stop = defaultMessageCount + 1; | ||
for (int x = 1; x < stop; x++) { | ||
String data = defaultMessage + "-" + x; | ||
|
||
Message msg = NatsMessage.builder() | ||
.subject(defaultSubjectSpecific) | ||
.data(data, StandardCharsets.UTF_8) | ||
.build(); | ||
System.out.printf("Publishing message %s on subject %s.\n", data, defaultSubjectSpecific); | ||
|
||
futures.add(js.publishAsync(msg)); | ||
} | ||
|
||
while (futures.size() > 0) { | ||
CompletableFuture<PublishAck> f = futures.remove(0); | ||
if (f.isDone()) { | ||
try { | ||
PublishAck pa = f.get(); | ||
System.out.printf("Publish Succeeded on subject %s, stream %s, seqno %d.\n", | ||
defaultSubjectSpecific, pa.getStream(), pa.getSeqno()); | ||
} catch (ExecutionException ee) { | ||
System.out.println("Publish Failed " + ee); | ||
} | ||
} else { | ||
futures.add(f); | ||
} | ||
} | ||
|
||
JetStreamSubscription sub = js.subscribe(defaultSubjectWildcard); | ||
List<Message> messages = new ArrayList<>(); | ||
Message msg = sub.nextMessage(Duration.ofSeconds(1)); | ||
boolean first = true; | ||
while (msg != null) { | ||
if (first) { | ||
first = false; | ||
System.out.print("Read/Ack ->"); | ||
} | ||
messages.add(msg); | ||
if (msg.isJetStream()) { | ||
msg.ack(); | ||
System.out.print(" " + new String(msg.getData()) + "\n"); | ||
} else if (msg.isStatusMessage()) { | ||
System.out.print(" !" + msg.getStatus().getCode() + "!"); | ||
} | ||
JsonUtils.printFormatted(msg.metaData()); | ||
msg = sub.nextMessage(Duration.ofSeconds(1)); | ||
} | ||
nc.flush(Duration.ZERO); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters