准备工作:
1.首先下载avtivemq的压缩包
2.解压
3.在bin目录下,有32位和64位系统不同的文件夹,根据自己的系统选择相应文件夹,执行activemq.bat脚本
一.普通方式实现点对点:
消息生产者:
package com.active;
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;
/**
* 消息生产者
* @author dk
*
*/
public class Producer {
private static final String USERNAME = ActiveMQConnectionFactory.DEFAULT_USER; //默认用户名
private static final String PASSWORD = ActiveMQConnectionFactory.DEFAULT_PASSWORD;//默认密码
private static final String BROKEURl = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;//默认地址
private static String queueName = "队列1";//消息队列名称
public static void main(String[] args) {
ConnectionFactory connectionFactory; //连接工厂
Connection connection = null ; //获取连接
Session session; //会话
Destination destination; //消息目的地
MessageProducer messageProducer; //消息生产者
//实例化连接
connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURl);
try {
connection = connectionFactory.createConnection();//通过工厂获取连接
connection.start();//启动连接
session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE );
destination = session.createQueue(queueName); //创建消息队列
messageProducer = session.createProducer(destination); //创建消息生产者
sendMessage(session, messageProducer);//发送消息
session.commit();//提交事务
Consumer.revice(queueName); //消费者接收消息
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(connection != null){
try {
connection.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
/*
* 发送消息
*/
public static void sendMessage(Session session,MessageProducer messageProducer) throws JMSException{
TextMessage textMessage = session.createTextMessage("测试1");
messageProducer.send(textMessage);
System.out.println("==========已发送消息"+textMessage.getText());
}
}
消息消费者:
package com.active;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息消费者
* @author dk
*
*/
public class Consumer {
private static final String USERNAME = ActiveMQConnectionFactory.DEFAULT_USER; //默认用户名
private static final String PASSWORD = ActiveMQConnectionFactory.DEFAULT_PASSWORD;//默认密码
private static final String BROKEURl = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;//默认地址
/*
* 接收消息
*/
public static void revice(String queue) {
ConnectionFactory connectionFactory; //连接工厂
Connection connection = null ; //获取连接
Session session; //会话
Destination destination; //消息目的地
MessageConsumer messageConsumer; //消息的消费者
//实例化连接
connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURl);
try {
connection = connectionFactory.createConnection();//通过工厂获取连接
connection.start();//启动连接
session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE );
destination = session.createQueue(queue); //创建消息队列
messageConsumer = session.createConsumer(destination); //创建消息消费者
TextMessage textMessage = (TextMessage) messageConsumer.receive();//接收消息
if(textMessage != null){
System.out.println("Active收到的消息:("+textMessage.getText()+")");
}else{
System.out.println("未收到消息");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(connection != null){
try {
connection.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
}
运行结果:
二.监听方式实现(推荐使用)
创建一个监听类,在消费者类里面注册即可
生产者类:
package com.active2;
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;
/**
* 消息生产者
* @author dk
*
*/
public class Producer2 {
private static final String USERNAME = ActiveMQConnectionFactory.DEFAULT_USER; //默认用户名
private static final String PASSWORD = ActiveMQConnectionFactory.DEFAULT_PASSWORD;//默认密码
private static final String BROKEURl = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;//默认地址
public static void main(String[] args) {
ConnectionFactory connectionFactory; //连接工厂
Connection connection = null ; //获取连接
Session session; //会话
Destination destination; //消息目的地
MessageProducer messageProducer; //消息生产者
//实例化连接
connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURl);
try {
connection = connectionFactory.createConnection();//通过工厂获取连接
connection.start();//启动连接
session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE );
destination = session.createQueue("one"); //创建消息队列
messageProducer = session.createProducer(destination); //创建消息生产者
sendMessage(session, messageProducer);//发送消息
session.commit();//提交事务
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(connection != null){
try {
connection.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
/*
* 发送消息
*/
public static void sendMessage(Session session,MessageProducer messageProducer) throws JMSException{
TextMessage textMessage = session.createTextMessage("2的消息");
messageProducer.send(textMessage);
System.out.println("==========已发送消息"+textMessage.getText());
}
}
监听类:
package com.active2;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class Listener implements MessageListener {
@Override
public void onMessage(Message arg0) {
try {
System.out.println("收到的消息"+((TextMessage)arg0).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
消费者类:
package com.active2;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
* 消息消费者
* @author dk
*
*/
public class Consumer2 {
private static final String USERNAME = ActiveMQConnectionFactory.DEFAULT_USER; //默认用户名
private static final String PASSWORD = ActiveMQConnectionFactory.DEFAULT_PASSWORD;//默认密码
private static final String BROKEURl = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;//默认地址
/*
* 接收消息
*/
public static void main(String[] args) {
ConnectionFactory connectionFactory; //连接工厂
Connection connection = null ; //获取连接
Session session; //会话
Destination destination; //消息目的地
MessageConsumer messageConsumer; //消息的消费者
//实例化连接
connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURl);
try {
connection = connectionFactory.createConnection();//通过工厂获取连接
connection.start();//启动连接
session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE );
destination = session.createQueue("one"); //创建消息队列
messageConsumer = session.createConsumer(destination); //创建消息消费者
messageConsumer.setMessageListener(new Listener());//注册监听消息
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(connection != null){
try {
connection.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
}