1.创建JMS项目文件
2.启动activeMQ服务端
以上有两个32,64位系统
activeMQ消息发送分为两种: 1.消息生产者 ,2.消息的消费者
3.创建JMSProducer(消息生产者)
package com.huangan.activemq;
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.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息生产者
* @author HuangAn
* @创建时间 2018年1月11日
* JMSProducer
*/
public class JMSProducer {
private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; //默认的连接用户名
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; //默认的连接密码
private static final String BROKER=ActiveMQConnection.DEFAULT_BROKER_URL; //默认的连接地址
private static final int SENDNUM=10; // 默认的消息数量
public static void main(String[] args){
ConnectionFactory connectionFactory; // 连接工厂
Connection connection = null; // 连接
Session session; // 会话 接受或者发送信息线程
Destination destination; // 消息的目的地
MessageProducer messageProducer; //消息生产者
//实例化连接工厂
connectionFactory =new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKER);
try {
connection=connectionFactory.createConnection(); //通过连接工厂获取连接
connection.start(); //启动连接
session=connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
destination=session.createQueue("FirstQueue1"); //创建消息队列
messageProducer=session.createProducer(destination); //创建消息生产者
try {
sendMessage(session,messageProducer);
session.commit(); //消息提交
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //发送消息
} 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();
}
}
}
}
/**
* 发送消息
* @param session
* @param messageProducer
* @throws Exception
*/
public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
for(int i=0; i<JMSProducer.SENDNUM;i++){
TextMessage message=session.createTextMessage("ActiveMQ 发送的消息"+i);
System.out.println("发送消息:"+i);
messageProducer.send(message);
}
}
}
4.创建JMSConsumer(消费者)package com.huangan.activemq;
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;
/**
* 消息消费者
* @author HuangAn
* @创建时间 2018年3月19日
* JMSConsumer
*/
public class JMSConsumer {
private static final String USERNAME=ActiveMQConnection.DEFAULT_USER; //默认的连接用户名
private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; //默认的连接密码
private static final String BROKER=ActiveMQConnection.DEFAULT_BROKER_URL; //默认的连接地址
private static final int SENDNUM=10; // 默认的消息数量
public static void main(String[] args){
ConnectionFactory connectionFactory; // 连接工厂
Connection connection = null; // 连接
Session session; // 会话 接受或者发送信息线程
Destination destination; // 消息的目的地
MessageConsumer messageConsumer; //消息的消费者
//实例化连接工厂
connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME,JMSConsumer.PASSWORD,JMSConsumer.BROKER);
try {
connection=connectionFactory.createConnection(); //通过连接工厂获取连接
connection.start(); //启动连接
session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
destination=session.createQueue("FirstQueue1"); //创建消息队列
messageConsumer=session.createConsumer(destination);//创建消息消费者
while(true){
TextMessage textMessgae=(TextMessage)messageConsumer.receive(100000);
if(textMessgae!=null){
System.err.println("收到的消息:"+textMessgae.getText());
}else{
break;
}
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试一下项目
1.点击JMSProducer.java文件运行main方法进入虚拟机
2.点击JMSConsumer.java文件运行main方法进入虚拟机获取消息
3.查看服务端的消息发送条数