在activeMQ官網下載activeMQ應用,並解壓:java
進入bin文件夾,雙擊activemq.bat啓動應用,發現沒法啓動,原來新版本須要點擊win64文件夾裏面的activemq.bat 。apache
啓動後打開ActiveMQ後臺管理地址: http://localhost:8161/admin,新建一個Queue(隊列)api
應用部署部分完成。session
代碼部分:tcp
須要從下載的activeMQ把activemq-all-5.14.4.jar,activemq-broker-5.14.4.jar,activemq-client-5.14.4.jar,geronimo-j2ee-management_1.1_spec-1.0.1.jar,geronimo-jms_1.1_spec-1.1.1.jar,slf4j-api-1.7.13.jar複製到java項目裏,並引用。code
新建消息發送者類Sender:隊列
package com.demo.activemq; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class Sender { public static void main(String[] args) { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); Connection connection = null; try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("MyQueue"); MessageProducer messageProducer = session.createProducer(destination); messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); sendMessage(session, messageProducer); session.commit(); } catch (JMSException e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } finally { if (null != connection) { try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } public static void sendMessage(Session session, MessageProducer producer) throws Exception { for (int i = 1; i <= 5; i++) { TextMessage message = session.createTextMessage("ActiveMq 發送的消息" + i); // 發送消息到目的地方 System.out.println("發送消息:" + "ActiveMq 發送的消息" + i); producer.send(message); } } }
新建消息接收者類Receiver:部署
package com.demo.activemq; 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 org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; public class Receiver { public static void main(String[] args) { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); Connection connection = null; try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("MyQueue"); MessageConsumer consumer = session.createConsumer(destination); while (true) { TextMessage message = (TextMessage) consumer.receive(100000); if (null != message) { System.out.println("收到消息" + message.getText()); } else { break; } } } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { ignore.printStackTrace(); } } } }
而後運行就能夠了。get