maven添加依赖
<dependency> <groupId>org.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.9.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.9.0</version> <exclusions> <exclusion> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jms_1.1_spec</artifactId> </exclusion> </exclusions> </dependency>
consumer
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class Consumer implements MessageListener { private String user = ActiveMQConnection.DEFAULT_USER; //默认 用户 private String password = ActiveMQConnection.DEFAULT_PASSWORD; //默认 密码 private String url = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的是localhost:8080 private String subject = "DEMO.TOPIC"; //消息目的地名称 private Topic topic = null;//在发布/订阅(PUB/SUB)消息传递域中,目的地被成为主题(topic)。 private Connection connection = null; //初始化 一个JMS客户端到JMS Provider的连接 private Session session = null; //初始化 一个接受消息的进程 private MessageConsumer consumer = null; //初始化 消息消费者 // 初始化 private void initialize() throws JMSException, Exception { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url); connection = connectionFactory.createConnection(); //false 参数表示 为非事务型消息,后面的参数表示消息的确认类型(见4.消息发出去后的确认模式) session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); topic = session.createTopic(subject); consumer = session.createConsumer(topic); } // 消费消息 public void consumeMessage() throws JMSException, Exception { initialize(); connection.start(); System.out.println("Consumer:->Begin listening..."); consumer.setMessageListener(this); } // 关闭连接 public void close() throws JMSException { System.out.println("Consumer:->Closing connection"); if (consumer != null) consumer.close(); if (session != null) session.close(); if (connection != null) connection.close(); } // 消息处理函数 public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage txtMsg = (TextMessage) message; String msg = txtMsg.getText(); System.out.println("Consumer:->Received: " + msg); } else { System.out.println("Consumer:->Received: " + message); } } catch (JMSException e) { e.printStackTrace(); } } }
Producer
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; public class Producer { private String user = ActiveMQConnection.DEFAULT_USER; //默认 用户 private String password = ActiveMQConnection.DEFAULT_PASSWORD; //默认 密码 private String url = ActiveMQConnection.DEFAULT_BROKER_URL; //默认的是localhost:8080 private String subject = "DEMO.TOPIC"; //消息目的地名称 private Topic topic = null;//在发布/订阅(PUB/SUB)消息传递域中,目的地被成为主题(topic)。 private Connection connection = null; //初始化 一个JMS客户端到JMS Provider的连接 private Session session = null; //初始化 一个发送消息的进程 private MessageProducer producer = null; //初始化 消息生产者 (它是由session 创建的) // 初始化 private void initialize() throws JMSException, Exception { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory( user, password, url); connection = connectionFactory.createConnection(); //false 参数表示 为非事务型消息,后面的参数表示消息的确认类型(见4.消息发出去后的确认模式) session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //在发布/订阅(PUB/SUB)消息,目的地被成为主题(topic)。 topic = session.createTopic(subject); producer = session.createProducer(topic); //消息模型为非持久型 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); } // 发送消息 public void produceMessage(String message) throws JMSException, Exception { initialize(); TextMessage msg = session.createTextMessage(message); connection.start(); System.out.println("Producer:->Sending message: " + message); producer.send(msg); System.out.println("Producer:->Message sent complete!"); } // 关闭连接 public void close() throws JMSException { System.out.println("Producer:->Closing connection"); if (producer != null) producer.close(); if (session != null) session.close(); if (connection != null) connection.close(); } }
TestTopic
public class TestTopic { public static void main(String[] args) throws Exception { Consumer consumer = new Consumer(); Producer producer = new Producer(); consumer.consumeMessage(); producer.produceMessage("Hello, topic!"); producer.close(); consumer.close(); } }