启动ActiveMQ,直接进入安装目录执行\bin\activemq
如下,启动成功
访问http://localhost:8161/admin/queues.jsp
消息生产者:
package activemq.yang;
//ConnectionFactory 是连接工厂,负责创建Connection。
//Connection 负责创建 Session。
//Session 创建 MessageProducer(用来发消息) 和 MessageConsumer(用来接收消息)。
//Destination 是消息的目的地。
import javax.jms.Connection;
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 Producer2 {
/**
* @param args
*/
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = "tcp://localhost:61616";
private String subject = "QDEFAULT";
private Destination destination = null;
private Connection connection = null;
private Session session = null;
private MessageProducer producer = null;
private static final int SEND_NUMBER = 5;
// 初始化
private void initialize() throws JMSException, Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
// 发送消息
public void produceMessage() throws JMSException, Exception {
initialize();
String text = "Hello World";
for (int i = 0; i < SEND_NUMBER; i++){
TextMessage msg = session.createTextMessage(text + i);
System.out.println("Producer:->Sending message: " + text + i);
this.producer.send(msg);
}
session.commit();
System.out.println("Producer:->Message sent complete!");
}
// 关闭连接
public void close() throws JMSException {
if (producer != null)
producer.close();
if (session != null)
session.close();
if (connection != null)
connection.close();
System.out.println("Producer:->Closing connection");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Producer2 producer = new Producer2();
try {
producer.produceMessage();
producer.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
消息消费者:
package activemq.yang;
import javax.jms.Connection;
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 Consumer2 {
/**
* @param args
*/
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = "tcp://localhost:61616";
private String subject = "QDEFAULT";
private Destination destination = null;
private Connection connection = null;
private Session session = null;
private MessageConsumer consumer = null;
// 初始化
private void initialize() throws JMSException, Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.FALSE.booleanValue(), Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
consumer = session.createConsumer(destination);
}
// 消费消息
public void consumeMessage() throws JMSException, Exception {
initialize();
System.out.println("Consumer:->Begin Receive...");
while (true) {
TextMessage message = (TextMessage) consumer.receive(1000);
if (null != message) {
System.out.println("Consumer:->Receiving message: " + message.getText());
} else {
System.out.println("Consumer:->Receive completed");
break;
}
}
}
// 关闭连接
public void close() throws JMSException {
if (consumer != null)
consumer.close();
if (session != null)
session.close();
if (connection != null)
connection.close();
System.out.println("Consumer:->Closing connection");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Consumer2 consumer = new Consumer2();
try {
consumer.consumeMessage();
consumer.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
执行代码,http://localhost:8161/admin/queues.jsp显示如下: