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.MessageListener;
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 connFac = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
Connection conn = null;
try {
conn = connFac.createConnection();
conn.start();
Session session = conn.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("myQueue");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
try {
System.out.println("RECEIVE[1] : "
+ ((TextMessage) msg).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
MessageConsumer consumer1 = session.createConsumer(destination);
consumer1.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
try {
System.out.println("RECEIVE[2] : "
+ ((TextMessage) msg).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
// 注释掉以保持监听状态
// finally {
// try {
// if (null != connection)
// connection.close();
// } catch (Throwable ignore) {
// }
// }
}
}
|