消息生产者
测试类
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 Producer {
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = "tcp://localhost:61616";
private String subject = "QDEFAULT2";
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");
}
}
消息消费者
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 Consumer {
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = "tcp://localhost:61616";
private String subject = "QDEFAULT2";
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(true, 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");
}
}
测试类
package activemq.yang;
import javax.jms.JMSException;
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws JMSException, Exception {
Consumer consumer = new Consumer();
Producer producer = new Producer();
producer.produceMessage();
producer.close();
consumer.consumeMessage();
consumer.close();
}
}
运行Test
监听界面:
因为设置session的的属性值为true,所以消息继续存在,改成false,再次执行