Introduce EzyMosquitto
Updated at 16998040250001. Overview
EzyMosquitto (Easy going to Mosquitto Interaction) is a framework support to interact to Apache Mosquitto, It supports:- RPC (Remote procedure call)
- Publish and consume message
- Serialize and Deserialize to map message's data in byte array to POJO
- Annotation driven
2. Structure of EzyMosquitto
EzyMosquittoProxy
: Manages producers, consumers, topics and data serializers.EzyMosquittoProducer
: Supports to send messages and RPC.EzyMosquittoProducer
: Supports to receive, consume the messages and response the RPC resultEzyMosquittoTopic
: Supports to send and consume messages via topic
3. Install EzyMosquitto
1. To create EzyMosquitto we need add dependency
<dependency> <groupId>com.tvd12</groupId> <artifactId>ezymq-mosquitto</artifactId> <version>1.2.6</version> </dependency>
The latest version can be found in the Maven Central repository. You can configure EzyMosquitto like this:
EzyMosquittoProxy proxy = EzyMosquittoProxy.builder()
.scan("com.tvd12.ezymq.mosquitto.test")
.build();
2. You can add to your configuration file like this:
# for application.yaml mosquitto: producers: test: default consumers: test: default topics: hello: producer: default consumer: default
# for application.properties mosquitto.producers.test=default mosquitto.consumers.test=default mosquitto.topics.hello.producer=default mosquitto.topics.hello.consumer=default
4. Example
Let’s say we need consume a message to sum 2 integer numbers and get the result, we can do it with EzyMosquitto follow by bellow steps.
4.1 Create request/response classes
We need create a request class has 2 fields: a
and b
:
package com.tvd12.ezymq.mosquitto.test.request; import com.tvd12.ezyfox.message.annotation.EzyMessage; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @EzyMessage @AllArgsConstructor @NoArgsConstructor public class SumRequest { private int a; private int b; }
And a response class has 1 field sum
:
package com.tvd12.ezymq.mosquitto.test.response; import com.tvd12.ezyfox.message.annotation.EzyMessage; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.ToString; @Getter @EzyMessage @ToString @AllArgsConstructor public class SumResponse { private int sum; }
4.2 Create message handler class
Now we need create a handler class to handle the message:
package com.tvd12.ezymq.mosquitto.test.handler; import com.tvd12.ezymq.mosquitto.annotation.EzyMosquittoHandler; import com.tvd12.ezymq.mosquitto.handler.EzyMosquittoRequestHandler; import com.tvd12.ezymq.mosquitto.test.request.SumRequest; import com.tvd12.ezymq.mosquitto.test.response.SumResponse; @EzyMosquittoHandler("sum") public class SumRequestHandler implements EzyMosquittoRequestHandler { @Override public Object handle(SumRequest request) { return new SumResponse( request.getA() + request.getB() ); } }
4.3 Create a program for rpc client
We need create a class has main
method to start a rpc client:
package com.tvd12.ezymq.mosquitto.test; import com.tvd12.ezymq.mosquitto.EzyMosquittoProxy; import com.tvd12.ezymq.mosquitto.EzyMosquittoRpcProducer; import com.tvd12.ezymq.mosquitto.test.request.SumRequest; import com.tvd12.ezymq.mosquitto.test.response.SumResponse; public final class SumRpcClientProgram { public static void main(String[] args) { final EzyMosquittoProxy proxy = EzyMosquittoProxy .builder() .scan("com.tvd12.ezymq.mosquitto.test") .build(); final EzyMosquittoRpcProducer producer = proxy.getRpcProducer("test"); SumResponse sumResponse = producer.call( "sum", new SumRequest(1, 2), SumResponse.class ); System.out.println("sum result: " + sumResponse); } }
With configration like this:
# for application.yaml mosquitto: producers: test: default
4.4 Create a program for rpc server
We need create a class has main
method to start consumers:
package com.tvd12.ezymq.mosquitto.test; import com.tvd12.ezymq.mosquitto.EzyMosquittoProxy; public final class SumRpcServerProgram { public static void main(String[] args) { EzyMosquittoProxy .builder() .scan("com.tvd12.ezymq.mosquitto.test") .build(); } }
With configration like this:
# for application.yaml mosquitto: consumers: test: default
4.5 Run the program
Please make sure you have downloaded, installed, and started the Mosquitto broker.
Now run the both SumRpcServerProgram
and SumRpcClientProgram
programs separately, we will see the log in the SumRpcClientProgram
:
sum result: SumResponse(sum=3)
Next step
You can see how to configure.