- Hands-On Cloud:Native Microservices with Jakarta EE
- Luigi Fugaro Mauro Vocale
- 295字
- 2021-07-02 13:47:10
Messaging communications
Java EE offers the ability to implement loosely coupled services that can communicate with each other, maintaining a guarantee of delivery and fault tolerance. This feature is implemented through the JMS that was introduced in Java EE since the beginning of the platform. The purpose of this specification is to decouple services and process messages, producing and consuming them asynchronously.
The new version of the specification, JMS 2.0, which was introduced in Java EE 7, simplifies the use of this specification and reduces the boilerplate that's needed to implement the message producers and consumers.
The following is an easy example of a message producer:
public class AuthorProducer {
@Inject
private JMSContext jmsContext;
@Resource(lookup = "jms/authorQueue")
private Destination queue;
/**
* Send Message.
*/
@Override
public void sendMessage(String message) {
/*
* createProducer: Creates a new JMSProducer object that will be used to
* configure and send messages.
*/
jmsContext.createProducer().send(queue, message);
}
}
Now, it's time to implement the consumer:
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "jms/authorQueue")})
public class AuthorMDB implements MessageListener {
/**
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message rcvMessage) {
TextMessage msg = null;
try {
if (rcvMessage instanceof TextMessage) {
msg = (TextMessage) rcvMessage;
System.out.println("Received Message from queue: " + msg.getText());
} else {
System.out.println("Message of wrong type: " + rcvMessage.getClass().getName());
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
}
It's easy to implement decoupling messaging services in Java EE.
Obviously, this is not enough to declare that the traditional Java EE approach is reactive-friendly.
But the approach we described above can help you update your traditional monolith to be less synchronous and less I/O blocking to better serve the new way to design enterprise and cloud-native architecture.
- Mastering Machine Learning for Penetration Testing
- 網(wǎng)絡(luò)故障現(xiàn)場(chǎng)處理實(shí)踐(第4版)
- Web Application Development with R Using Shiny
- 新一代物聯(lián)網(wǎng)架構(gòu)技術(shù):分層算力網(wǎng)絡(luò)
- PLC、現(xiàn)場(chǎng)總線及工業(yè)網(wǎng)絡(luò)實(shí)用技術(shù)速成
- 企業(yè)網(wǎng)絡(luò)安全管理
- C/C++串口通信:典型應(yīng)用實(shí)例編程實(shí)踐
- Working with Legacy Systems
- Building Web Applications with ArcGIS
- 局域網(wǎng)組成實(shí)踐
- 大型企業(yè)微服務(wù)架構(gòu)實(shí)踐與運(yùn)營
- 基于IPv6的家居物聯(lián)網(wǎng)開發(fā)與應(yīng)用技術(shù)
- Python Web Scraping Cookbook
- 互聯(lián)網(wǎng)安全的40個(gè)智慧洞見(2018)
- 物聯(lián)網(wǎng)商業(yè)設(shè)計(jì)與案例