Java消息中间件JMS模式与代码示例详解

下载需积分: 9 | ZIP格式 | 22KB | 更新于2024-12-31 | 21 浏览量 | 1 下载量 举报
收藏
资源摘要信息: "Java消息服务(JMS)测试套件" JMS(Java Message Service)是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。JMS是Java EE的一部分,提供了创建、发送、接收消息的标准API。JMS支持两种消息传递模式:点对点(Point-to-Point,PTP)和发布/订阅(Publish/Subscribe,Pub/Sub)。 1. 点对点(PTP)模式: 在点对点消息传递模型中,消息生产者(Producer)发送消息到目标对象,然后消息消费者(Consumer)从目标对象中检索消息。该模型适用于一对一的消息传递场景。PTP模型中,消息被发送到一个队列(Queue),每个消息只会被一个消费者接收一次。 2. 发布/订阅(Pub/Sub)模式: 发布/订阅模型允许多个消费者订阅消息主题(Topic),当生产者发送消息到该主题时,所有订阅者都将接收到该消息的副本。这种模式适用于一对多的消息传递场景,比如通知所有用户更新信息。 以下是对这两种消息模式的具体说明和代码示例: 1. 点对点模型代码示例: 在点对点模型中,主要包含两个实体:QueueSender(消息生产者)和QueueReceiver(消息消费者)。消息生产者将消息发送到指定的队列,消息消费者从队列中取出消息进行处理。 QueueSender.java: ```java import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.InitialContext; public class QueueSender { public static void main(String[] args) throws JMSException, NamingException { InitialContext ctx = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); Destination queue = (Destination) ctx.lookup("queue/myQueue"); Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer sender = session.createProducer(queue); TextMessage message = session.createTextMessage("Hello World"); sender.send(message); sender.close(); session.close(); connection.close(); } } ``` QueueReceiver.java: ```java import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.InitialContext; public class QueueReceiver { public static void main(String[] args) throws JMSException, NamingException { InitialContext ctx = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); Destination queue = (Destination) ctx.lookup("queue/myQueue"); Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer receiver = session.createConsumer(queue); connection.start(); TextMessage message = (TextMessage) receiver.receive(); System.out.println("Received message: " + message.getText()); receiver.close(); session.close(); connection.close(); } } ``` 2. 发布/订阅模型代码示例: 在发布/订阅模型中,主要包含两个实体:TopicPublisher(消息发布者)和TopicSubscriber(消息订阅者)。消息发布者将消息发送到特定主题,所有订阅该主题的消息订阅者都会收到消息。 TopicPublisher.java: ```java import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.InitialContext; public class TopicPublisher { public static void main(String[] args) throws JMSException, NamingException { InitialContext ctx = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); Destination topic = (Destination) ctx.lookup("topic/myTopic"); Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer sender = session.createProducer(topic); TextMessage message = session.createTextMessage("Hello World"); sender.send(message); sender.close(); session.close(); connection.close(); } } ``` TopicSubscriber.java: ```java import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.InitialContext; public class TopicSubscriber { public static void main(String[] args) throws JMSException, NamingException { InitialContext ctx = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory"); Destination topic = (Destination) ctx.lookup("topic/myTopic"); Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer subscriber = session.createConsumer(topic); connection.start(); TextMessage message = (TextMessage) subscriber.receive(); System.out.println("Received message: " + message.getText()); subscriber.close(); session.close(); connection.close(); } } ``` 通过这两个模型的代码示例,我们可以看到JMS在Java中的应用方式,了解如何在企业应用中实现松耦合的消息传递。这有助于构建能够处理高并发和消息积压的健壮应用。此外,掌握JMS对于任何使用Java EE技术栈的开发人员来说都是必备技能。

相关推荐