spring-boot-rocketmq-starter

Open Source Spring Boot Starter for Apache RocketMQ, develop with RocketMQ easily

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

io.github.rhwayfun
ArtifactId

ArtifactId

spring-boot-rocketmq-starter
Last Version

Last Version

0.0.6.RELEASE
Release Date

Release Date

Type

Type

jar
Description

Description

spring-boot-rocketmq-starter
Open Source Spring Boot Starter for Apache RocketMQ, develop with RocketMQ easily
Project URL

Project URL

https://rhwayfun.github.io/spring-boot-rocketmq-starter/
Source Code Management

Source Code Management

https://github.com/rhwayfun/spring-boot-rocketmq-starter

Download spring-boot-rocketmq-starter

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.rhwayfun/spring-boot-rocketmq-starter/ -->
<dependency>
    <groupId>io.github.rhwayfun</groupId>
    <artifactId>spring-boot-rocketmq-starter</artifactId>
    <version>0.0.6.RELEASE</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.rhwayfun/spring-boot-rocketmq-starter/
implementation 'io.github.rhwayfun:spring-boot-rocketmq-starter:0.0.6.RELEASE'
// https://jarcasting.com/artifacts/io.github.rhwayfun/spring-boot-rocketmq-starter/
implementation ("io.github.rhwayfun:spring-boot-rocketmq-starter:0.0.6.RELEASE")
'io.github.rhwayfun:spring-boot-rocketmq-starter:jar:0.0.6.RELEASE'
<dependency org="io.github.rhwayfun" name="spring-boot-rocketmq-starter" rev="0.0.6.RELEASE">
  <artifact name="spring-boot-rocketmq-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.rhwayfun', module='spring-boot-rocketmq-starter', version='0.0.6.RELEASE')
)
libraryDependencies += "io.github.rhwayfun" % "spring-boot-rocketmq-starter" % "0.0.6.RELEASE"
[io.github.rhwayfun/spring-boot-rocketmq-starter "0.0.6.RELEASE"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter jar 1.5.1.RELEASE
org.springframework.boot : spring-boot-configuration-processor jar 1.5.1.RELEASE
org.apache.rocketmq : rocketmq-client jar 4.1.0-incubating

test (1)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 1.5.1.RELEASE

Project Modules

There are no modules declared in this project.

Spring Boot RocketMQ Starter

Build Status Coverage Status Maven Central License

Open Source Spring Boot Starter for Apache RocketMQ, develop with RocketMQ easily.

中文版

Quick Start

Maven Dependency

    <dependency>
        <groupId>io.github.rhwayfun</groupId>
        <artifactId>spring-boot-rocketmq-starter</artifactId>
        <version>0.0.3.RELEASE</version>
    </dependency>

Configuration

You just need to add a few configurations on application.properties, configurations are like as follows:

# Apache RocketMQ
spring.rocketmq.nameServer=localhost:9876
spring.rocketmq.producer-group-name=spring-boot-test-producer-group

Look, it's easy, but in the real environment, you need modify above configurations.

Consume message

For consume message, just inherit from class io.github.rhwayfun.springboot.rocketmq.starter.common.AbstractRocketMqConsumer

Example:

@Component
    public class DemoMqConsumer extends AbstractRocketMqConsumer<DemoMqTopic, DemoMqContent> {


        @Override
        public boolean consumeMsg(RocketMqContent content, MessageExt msg) {
            System.out.println(new Date() + ", " + content);
            return true;
        }

        @Override
        public Map<String, Set<String>> subscribeTopicTags() {
            Map<String, Set<String>> map = new HashMap<>();
            Set<String> tags = new HashSet<>();
            tags.add("test-tag");
            map.put("test-topic", tags);
            return map;
        }

        @Override
        public String getConsumerGroup() {
            return "test-consumer-group";
        }
    }

    public class DemoMqTopic implements RocketMqTopic{

        @Override
        public String getTopic() {
            return "test-topic";
        }
    }

    public class DemoMqContent extends RocketMqContent {
        private int id;
        private String desc;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }

Send message

We encapsulate the part of the message sent for common use. The default implementation is DefaultRocketMqProducer.

if you need send message with RocketMQ, autowire this bean in your application.

example:

@Component
public class DemoRocketMqProducerExample {

    @Resource
    private DefaultRocketMqProducer producer; //this bean is provided by default.

    @PostConstruct
    public void execute() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                DemoRocketMqContent content = new DemoRocketMqContent();
                content.setCityId(1);
                content.setDesc("城市");
                Message msg = new Message("TopicA", "TagA", content.toString().getBytes());
                boolean sendResult = producer.sendMsg(msg);
                System.out.println("发送结果:" + sendResult);
            }
        }, 0, 10000);
    }

}

More configurations

num config description default
1 spring.rocketmq.nameServer name server
2 spring.rocketmq.producerGroupName name of producer
3 spring.rocketmq.producerSendMsgTimeout millis of send message timeout 3000
4 spring.rocketmq.producerCompressMsgBodyOverHowMuch Compress message body threshold 4000
5 spring.rocketmq.producerRetryTimesWhenSendFailed Maximum number of retry to perform internally before claiming sending failure in synchronous mode 2
6 spring.rocketmq.producerRetryTimesWhenSendAsyncFailed Maximum number of retry to perform internally before claiming sending failure in asynchronous mode 2
7 spring.rocketmq.producerRetryAnotherBrokerWhenNotStoreOk Indicate whether to retry another broker on sending failure internally false
8 spring.rocketmq.producerMaxMessageSize Maximum allowed message size in bytes 1024 * 4

License

Adopting the Apache License 2.0 protocol for licensing

Versions

Version
0.0.6.RELEASE
0.0.5.RELEASE
0.0.4.RELEASE
0.0.4
0.0.3.RELEASE
0.0.2.RELEASE
0.0.2
0.0.1.RELEASE
0.0.1