发送者:
package com.zzj.p2p;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Producer {
private static final String DEST = "com.zzj.dest.01";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, ActiveMQConnection.DEFAULT_BROKER_URL);
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(DEST);
MessageProducer producer = session.createProducer(queue);
Message message = session.createTextMessage("Hello world!");
producer.send(message);
System.out.println("发送成功!");
// 关闭资源
producer.close();
session.close();
connection.close();
}
}
同步接收者:
package com.zzj.p2p;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 同步消费
* @author lenovo
*
*/
public class SyncCustomer {
private static final String DEST = "com.zzj.dest.01";
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, ActiveMQConnection.DEFAULT_BROKER_URL);
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(DEST);
MessageConsumer consumer = session.createConsumer(queue);
TextMessage message = (TextMessage) consumer.receive(); // 阻塞方法,直到接收到消息
System.out.println(message.getText());
// 关闭资源
consumer.close();
session.close();
connection.close();
}
}
异步接收者:
package com.zzj.p2p;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 异步消费
* @author lenovo
*
*/
public class AsynCustomer implements MessageListener{
private static final String DEST = "com.zzj.dest.01";
private static Connection connection;
private static Session session;
private static MessageConsumer consumer;
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, ActiveMQConnection.DEFAULT_BROKER_URL);
connection = factory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(DEST);
consumer = session.createConsumer(queue);
consumer.setMessageListener(new AsynCustomer());
}
@Override
public void onMessage(Message message) {
TextMessage tm = (TextMessage) message;
try {
System.out.println(tm.getText());
} catch (JMSException e) {
e.printStackTrace();
close(); // 关闭资源
}
}
/**
* 关闭资源
*/
private static void close(){
try {
consumer.close();
} catch (JMSException e1) {
e1.printStackTrace();
}
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}