jms即Java Message Server(Java消息服务)在生活中就好像是顺风,申通,圆通一样,像是个快递机制,我们把快递交给快递员,快递员负责将你想要发的东西通过他自己的方式发送到目的地,jms有两种方式p2p,即点对点;和发布订阅两种方式
消息生产者产生消息发送到消息队列,然后等待消费者来取走消息
接下来是点对点的方式:
1.创建连接工厂
2.创建连接
3.打开连接
4.创建会话
5.创建消息生产者/消息消费者
6.生产者生产消息/消费者取走消息
生产者
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MsgTopicProducter {
private static final String user = ActiveMQConnectionFactory.DEFAULT_USER;//默认用户
private static final String password = ActiveMQConnectionFactory.DEFAULT_PASSWORD;//默认密码
private static final String url = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;//默认URL
public static void main(String[] args) {
//1 amq工厂
ConnectionFactory connectionFactory;
//2 amq连接
Connection connection = null;
//开启连接
//3 amq会话
Session session;
//4 目的地
Destination destination;
//5 amq生产者
MessageProducer messageProducer;
connectionFactory = new ActiveMQConnectionFactory(user,password,url);//创建工厂
try {
connection = connectionFactory.createConnection();//初始化连接
connection.start();//开启连接
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);//参数1位true时使用默认事务机制,第二个参数无效;参数1为false时,第二个参数有效消息说明下面叙述
destination = session.createQueue("queue1");//创建订阅队列目的地
messageProducer = session.createProducer(destination);
sendMessage(session,messageProducer);
session.commit();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection != null){
try {
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void sendMessage(Session session,MessageProducer messageProducer){
for (int i = 0; i < 10; i++) {
try {
TextMessage textMessage = session.createTextMessage("ActiveMQ发送的消息 "+i);
System.out.println(textMessage.getText());
messageProducer.send(textMessage);
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
消费者:
public class MsgConsumer {
private static final String userName = ActiveMQConnectionFactory.DEFAULT_USER;
private static final String password = ActiveMQConnectionFactory.DEFAULT_PASSWORD;
private static final String url = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionFactory connectionFactory;
Connection connection = null;
Session session;
Destination destination;
MessageConsumer createConsumer;
connectionFactory = new ActiveMQConnectionFactory(userName, password, url);
try {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("queue1");//必须和生产者目的地相同
createConsumer = session.createConsumer(destination);
while(true){
TextMessage textMessage = (TextMessage) createConsumer.receive(10000);
if(textMessage != null){
System.out.println("收到消息:"+textMessage.getText());
}else{
break;
}
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection!=null){
try {
connection.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
第二种方式监听器
不说了直接上代码,一定要先运行监听器,他会一直运行,再运行生产者产生消息
监听器
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class QueueListener implements MessageListener {
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
try {
System.out.println("监听器接收:"+((TextMessage)message).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
监听器方式消费消息,设置消息消费者的消息监听参数为上面定义的监听器,消息产生者和非监听方式一样
createConsumer.setMessageListener(new QueueListener());//监听方式,监听指定目的地的消息