mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
update rocketmq examples
This commit is contained in:
parent
5b2d068f7d
commit
7cc3b02917
@ -34,7 +34,8 @@
|
||||
<module>fescar-example/storage-service</module>
|
||||
<module>fescar-example/account-service</module>
|
||||
<module>acm-example/acm-local-example</module>
|
||||
<module>rocketmq-example</module>
|
||||
<module>rocketmq-example/rocketmq-consume-example</module>
|
||||
<module>rocketmq-example/rocketmq-produce-example</module>
|
||||
<module>sms-example</module>
|
||||
<module>spring-cloud-bus-rocketmq-example</module>
|
||||
<module>schedulerx-example/schedulerx-simple-task-example</module>
|
||||
|
@ -6,14 +6,14 @@
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-examples</artifactId>
|
||||
<version>0.2.2.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<artifactId>rocketmq-example</artifactId>
|
||||
<artifactId>rocketmq-consume-example</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<description>Example demonstrating how to use rocketmq</description>
|
||||
<description>Example demonstrating how to use rocketmq consume</description>
|
||||
|
||||
|
||||
<dependencies>
|
||||
@ -32,11 +32,6 @@
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.dropwizard.metrics</groupId>
|
||||
<artifactId>metrics-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
@ -25,9 +25,9 @@ public class ReceiveService {
|
||||
System.out.println("input3 receive: " + foo);
|
||||
}
|
||||
|
||||
@StreamListener("input4")
|
||||
public void receiveTransactionalMsg(String transactionMsg) {
|
||||
System.out.println("input4 receive transaction msg: " + transactionMsg);
|
||||
}
|
||||
@StreamListener("input4")
|
||||
public void receiveTransactionalMsg(String transactionMsg) {
|
||||
System.out.println("input4 receive transaction msg: " + transactionMsg);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package org.springframework.cloud.alibaba.cloud.examples;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.alibaba.cloud.examples.RocketMQConsumerApplication.MySink;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Input;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableBinding({ MySink.class })
|
||||
public class RocketMQConsumerApplication {
|
||||
|
||||
public interface MySink {
|
||||
|
||||
@Input("input1")
|
||||
SubscribableChannel input1();
|
||||
|
||||
@Input("input2")
|
||||
SubscribableChannel input2();
|
||||
|
||||
@Input("input3")
|
||||
SubscribableChannel input3();
|
||||
|
||||
@Input("input4")
|
||||
SubscribableChannel input4();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RocketMQConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -1,13 +1,4 @@
|
||||
spring.cloud.stream.default-binder=rocketmq
|
||||
|
||||
spring.cloud.stream.rocketmq.binder.namesrv-addr=127.0.0.1:9876
|
||||
|
||||
spring.cloud.stream.bindings.output1.destination=test-topic
|
||||
spring.cloud.stream.bindings.output1.content-type=application/json
|
||||
|
||||
spring.cloud.stream.bindings.output2.destination=TransactionTopic
|
||||
spring.cloud.stream.bindings.output2.content-type=application/json
|
||||
spring.cloud.stream.rocketmq.bindings.output2.producer.transactional=true
|
||||
spring.cloud.stream.rocketmq.binder.name-server=127.0.0.1:9876
|
||||
|
||||
spring.cloud.stream.bindings.input1.destination=test-topic
|
||||
spring.cloud.stream.bindings.input1.content-type=text/plain
|
||||
@ -33,9 +24,9 @@ spring.cloud.stream.bindings.input4.content-type=text/plain
|
||||
spring.cloud.stream.bindings.input4.group=transaction-group
|
||||
spring.cloud.stream.bindings.input4.consumer.concurrency=5
|
||||
|
||||
spring.application.name=rocketmq-example
|
||||
spring.application.name=rocketmq-consume-example
|
||||
|
||||
server.port=28081
|
||||
server.port=28082
|
||||
|
||||
management.endpoints.web.exposure.include=*
|
||||
management.endpoint.health.show-details=always
|
@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-examples</artifactId>
|
||||
<version>0.2.2.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<artifactId>rocketmq-produce-example</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<description>Example demonstrating how to use rocketmq produce</description>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>${maven-deploy-plugin.version}</version>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,39 @@
|
||||
package org.springframework.cloud.alibaba.cloud.examples;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
public class Foo {
|
||||
|
||||
private int id;
|
||||
private String bar;
|
||||
|
||||
public Foo() {
|
||||
}
|
||||
|
||||
public Foo(int id, String bar) {
|
||||
this.id = id;
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
public void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Foo{" + "id=" + id + ", bar='" + bar + '\'' + '}';
|
||||
}
|
||||
}
|
@ -4,36 +4,18 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.alibaba.cloud.examples.RocketMQApplication.MySink;
|
||||
import org.springframework.cloud.alibaba.cloud.examples.RocketMQApplication.MySource;
|
||||
import org.springframework.cloud.alibaba.cloud.examples.RocketMQProduceApplication.MySource;
|
||||
import org.springframework.cloud.stream.annotation.EnableBinding;
|
||||
import org.springframework.cloud.stream.annotation.Input;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableBinding({ MySource.class, MySink.class })
|
||||
public class RocketMQApplication {
|
||||
|
||||
public interface MySink {
|
||||
|
||||
@Input("input1")
|
||||
SubscribableChannel input1();
|
||||
|
||||
@Input("input2")
|
||||
SubscribableChannel input2();
|
||||
|
||||
@Input("input3")
|
||||
SubscribableChannel input3();
|
||||
|
||||
@Input("input4")
|
||||
SubscribableChannel input4();
|
||||
}
|
||||
@EnableBinding({ MySource.class })
|
||||
public class RocketMQProduceApplication {
|
||||
|
||||
public interface MySource {
|
||||
@Output("output1")
|
||||
@ -44,7 +26,7 @@ public class RocketMQApplication {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RocketMQApplication.class, args);
|
||||
SpringApplication.run(RocketMQProduceApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ -86,13 +68,13 @@ public class RocketMQApplication {
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
// COMMIT_MESSAGE message
|
||||
senderService.sendTransactionalMsg("transactional-msg1", false);
|
||||
senderService.sendTransactionalMsg("transactional-msg1", 1);
|
||||
// ROLLBACK_MESSAGE message
|
||||
senderService.sendTransactionalMsg("transactional-msg2", true);
|
||||
senderService.sendTransactionalMsg("transactional-msg2", 2);
|
||||
// ROLLBACK_MESSAGE message
|
||||
senderService.sendTransactionalMsg("transactional-msg3", true);
|
||||
senderService.sendTransactionalMsg("transactional-msg3", 3);
|
||||
// COMMIT_MESSAGE message
|
||||
senderService.sendTransactionalMsg("transactional-msg4", false);
|
||||
senderService.sendTransactionalMsg("transactional-msg4", 4);
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,9 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.rocketmq.common.message.MessageConst;
|
||||
import org.apache.rocketmq.spring.support.RocketMQHeaders;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.alibaba.cloud.examples.RocketMQApplication.MySource;
|
||||
import org.springframework.cloud.alibaba.cloud.examples.RocketMQProduceApplication.MySource;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
@ -40,12 +41,11 @@ public class SenderService {
|
||||
source.output1().send(message);
|
||||
}
|
||||
|
||||
public <T> void sendTransactionalMsg(T msg, boolean error) throws Exception {
|
||||
public <T> void sendTransactionalMsg(T msg, int num) throws Exception {
|
||||
MessageBuilder builder = MessageBuilder.withPayload(msg)
|
||||
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON);
|
||||
if (error) {
|
||||
builder.setHeader("test", "1");
|
||||
}
|
||||
builder.setHeader("test", String.valueOf(num));
|
||||
builder.setHeader(RocketMQHeaders.TAGS, "binder");
|
||||
Message message = builder.build();
|
||||
source.output2().send(message);
|
||||
}
|
@ -27,13 +27,20 @@ import org.springframework.messaging.Message;
|
||||
/**
|
||||
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
||||
*/
|
||||
@RocketMQTransactionListener(txProducerGroup = "TransactionTopic", corePoolSize = 5, maximumPoolSize = 10)
|
||||
class TransactionListenerImpl implements RocketMQLocalTransactionListener {
|
||||
@RocketMQTransactionListener(txProducerGroup = "myTxProducerGroup", corePoolSize = 5, maximumPoolSize = 10)
|
||||
public class TransactionListenerImpl implements RocketMQLocalTransactionListener {
|
||||
@Override
|
||||
public RocketMQLocalTransactionState executeLocalTransaction(Message msg,
|
||||
Object arg) {
|
||||
if ("1".equals(((HashMap) msg.getHeaders().get(RocketMQHeaders.PROPERTIES))
|
||||
.get("USERS_test"))) {
|
||||
Object num = ((HashMap) msg.getHeaders().get(RocketMQHeaders.PROPERTIES))
|
||||
.get("USERS_test");
|
||||
|
||||
if ("1".equals(num)) {
|
||||
System.out.println(
|
||||
"executer: " + new String((byte[]) msg.getPayload()) + " unknown");
|
||||
return RocketMQLocalTransactionState.UNKNOWN;
|
||||
}
|
||||
else if ("2".equals(num)) {
|
||||
System.out.println(
|
||||
"executer: " + new String((byte[]) msg.getPayload()) + " rollback");
|
||||
return RocketMQLocalTransactionState.ROLLBACK;
|
@ -0,0 +1,20 @@
|
||||
logging.level.org.springframework.cloud.stream.binder.rocketmq=DEBUG
|
||||
|
||||
spring.cloud.stream.rocketmq.binder.name-server=127.0.0.1:9876
|
||||
|
||||
spring.cloud.stream.bindings.output1.destination=test-topic
|
||||
spring.cloud.stream.bindings.output1.content-type=application/json
|
||||
spring.cloud.stream.rocketmq.bindings.output1.producer.group=binder-group
|
||||
spring.cloud.stream.rocketmq.bindings.output1.producer.sync=true
|
||||
|
||||
spring.cloud.stream.bindings.output2.destination=TransactionTopic
|
||||
spring.cloud.stream.bindings.output2.content-type=application/json
|
||||
spring.cloud.stream.rocketmq.bindings.output2.producer.transactional=true
|
||||
spring.cloud.stream.rocketmq.bindings.output2.producer.group=myTxProducerGroup
|
||||
|
||||
spring.application.name=rocketmq-produce-example
|
||||
|
||||
server.port=28081
|
||||
|
||||
management.endpoints.web.exposure.include=*
|
||||
management.endpoint.health.show-details=always
|
Loading…
x
Reference in New Issue
Block a user