从前有个山,山上有个庙,庙里有个大帅哥正在看我的博客。
不多说,上代码!
这是消息发送端的代码
package com.example.demo.activitemp;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
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 ShakeDownActiviteMQ {
public static void main(String[] args) throws InterruptedException {
Thread tl = new Thread(new Producer());
tl.start();
}
}
class Producer implements Runnable{
public static final int SEND_NUM = 5 ;
@Override
public void run() {
ConnectionFactory connectionFactory ;
Connection connection = null ;
Session session = null;
Destination destination ;
MessageProducer messageProducer ;
try {
//创建工厂连接
connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");
//通过工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//获得连接的session
session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE );
//获取连接目的地
destination = session.createQueue("cxx");
//创建消息生产者
messageProducer = session.createProducer(destination);
//支持消息持久化
messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
//发送消息
sendMsg(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();
}
}
if(session != null){
try {
session.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void sendMsg(Session session,MessageProducer producer) throws JMSException{
for(int i = 1 ; i <= SEND_NUM ; i++){
TextMessage textMessage = session.createTextMessage("添加第"+i+"几张票");
System.out.println(textMessage);
producer.send(textMessage);
}
}
}
这是消息接收端的代码
package com.example.demo.activitemp;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
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;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
public class Consumer implements Runnable{
public static final int SEND_NUM = 5 ;
public static void main(String[] args) throws InterruptedException {
Thread tl = new Thread(new Consumer());
tl.start();
}
@Override
public void run() {
ConnectionFactory connectionFactory ;
Connection connection = null ;
Session session = null;
Destination destination ;
MessageConsumer consumer ;
try {
//创建工厂的连接
connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");
//通过工厂获取连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//使用连接创建一个会话
session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
//获取session的队列
destination = session.createQueue("cxx");
//创建一个消费者
consumer = session.createConsumer(destination);
TextMessage tm = (TextMessage)consumer.receive(1000);
while(true){
if(tm != null){
System.out.println(tm.getText()+"恭喜------抢票成功");
}else{
System.out.println("抢票失败");
break ;
}
}
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();
}
}
if(session != null){
try {
session.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
运行发送端之后
运行消息接收端之后
然后打开 帐号admin 密码 admin 看看自己的消息
http://127.0.0.1:8161/admin/queues.jsp
写到一半又有新任务了,同志们,加油吧!!!