1,ActiveMQ下载
2,开始ActiveMQ
(1)
在控制台cd [activimq_install_dir]/bin/activemq start
(2)
在浏览器进入http://localhost:8161/admin到ActiveMQ的管理页面 账号:admin 密码:admin
(3)
切换到Queues导航,创建一个Queue,输入一个name点击create
(4)点击send to输入MessageBody内容点击send可以在
[activimq_install_dir]/data/activemq.log中出现
Apache ActiveMQ 5.14.0 (localhost, ID:DESKTOP-FBFV2OK-57402-1472799279718-0:1) is starting | org.apache.activemq.broker.BrokerService | main
3,创建java项目
package com.lql.activimqtest;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Producer {
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory;
Connection connection = null;
Session session = null;
MessageConsumer consumer = null;
try {
// Create a ConnectionFactory
connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
// Create a Connection
connection = connectionFactory.createConnection();
connection.start();
// Create a Session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("MyQueue");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
sendMessage(session, producer);
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
} finally {
session.close();
connection.close();
}
}
private static void sendMessage(Session session, MessageProducer producer) throws Exception {
// TODO Auto-generated method stub
for(int i = 0; i < 10; i++) {
// 这里将线程停顿1s,为了使结果更明显
Thread.sleep(1000);
String text = "Hello world! From: Producer " + i;
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Sent message: "+ i);
producer.send(message);
}
}
}
package com.lql.activimqtest;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Consumer {
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory;
Connection connection = null;
Session session = null;
MessageConsumer consumer = null;
// Create a ConnectionFactory
connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
try {
// Create a Connection
connection = connectionFactory.createConnection();
connection.start();
// Create a Session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("MyQueue");
// Create a MessageConsumer from the Session to the Topic or Queue
consumer = session.createConsumer(destination);
while(true) {
// Wait for a message
Message message = consumer.receive(100);
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
}
}
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
} finally {
consumer.close();
session.close();
connection.close();
}
}
}
4,运行项目
首先运行Consumer,控制台会处于等待状态
然后运行Producer,控制台每秒输出一次,同时Consumer的控制台也会出现打印输出
运行结果如图
Producer:
Consumer
在http://localhost:8161/admin/queues.jsp中可以看到
一共发送10条消息,接收10条消息