activitemq整合spring
一.activmq的点对点模型
pom.xml:
4.0.0
com.demo
aq-test
1.0-SNAPSHOT
war
aq-test Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
junit
junit
4.12
test
javax.jms
jms-api
1.1-rev-1
org.apache.activemq
activemq-all
5.14.5
junit
junit
4.12
mysql
mysql-connector-java
5.1.34
org.springframework
spring-aop
5.1.3.RELEASE
org.apache.commons
commons-pool2
2.6.0
org.apache.commons
commons-dbcp2
2.1.1
aq-test
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
ActiviteMq.class:(发送端)
package com.demo;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;
import javax.jms.*;
public class ActiviteMq {
@Test
public void testQueueProducer() throws JMSException {
//1.创建connectinfactory对象,需要指定服务的IP以及端口号
//brokerURL服务器的ip以及端口号
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"tcp://192.168.1.20:61616"
);
//2.使用ConnectionFactory创建
Connection connection = connectionFactory.createConnection();
//3.开启链接,调用connection对象的start的方法
connection.start();
//4.使用connection对创建一个session对象
//[4.1] 第一参数:是否开启事务
//[4.2] 第二参数:当第一个参数为false的时候 才有意义 消息的应答模式
//1.自动应答2.手动应答 一般为自动
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//第五步:使用session对象创建一个destination对象(topic,queue) 此处创建一个queue对象
//参数:队列名称
Queue queue = session.createQueue("test-queue2");
//第六步 使用session创建一个producer对象
MessageProducer producer = session.createProducer(queue);
//第七步 创建一个message对象 创建一个textmessage对象
TextMessage textMessage = session.createTextMessage("风风光光");
//第八步 使用producer对象发送消息
producer.send(textMessage);
//第九步 关闭资源
producer.close();
session.close();
connection.close();
}
}
ReceiveMsf.class:(接收端)
package com.demo;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;
import javax.jms.*;
import java.io.IOException;
public class ReceiveMsf {
@Test
public void testQueueConsumer() throws JMSException, IOException {
//1.创建connectinfactory对象,需要指定服务的IP以及端口号
//brokerURL服务器的ip以及端口号
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"tcp://192.168.1.20:61616"
);
//2.使用ConnectionFactory创建
Connection connection = connectionFactory.createConnection();
//3.开启链接,调用connection对象的start的方法
connection.start();
//4.使用connection对创建一个session对象
//[4.1] 第一参数:是否开启事务
//[4.2] 第二参数:当第一个参数为false的时候 才有意义 消息的应答模式
//1.自动应答2.手动应答 一般为自动
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//第五步:使用session对象创建一个destination对象(topic,queue) 此处创建一个queue对象
//参数:队列名称
Queue queue = session.createQueue("test-queue2");
// 第六步:使用Session对象创建一个Consumer对象。
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(
new MessageListener() {
@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
String text = null;
//取消的内容
text = textMessage.getText();
//第八步 打印消息
System.out.println(text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
);
//等待键盘输入 阻塞
System.in.read();
//第九步 关闭资源
consumer.close();
session.close();
connection.close();
}
}
二.activmq的发布订阅模型
TopicProducer.class
package com.demo.dingyue;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;
import javax.jms.*;
public class TopicProducer {
@Test
public void testTopicProducer() throws JMSException {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"tcp://192.168.1.20:61616"
);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("huaYuanBaoBao");
MessageProducer producer = session.createProducer(topic);
TextMessage textMessage = session.createTextMessage("这个是发布订阅的");
producer.send(textMessage);
producer.close();
session.close();
connection.close();
}
}
TopicCustomer.class:
package com.demo.dingyue;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;
import javax.jms.*;
import java.io.IOException;
public class TopicCustomer {
@Test
public void testTopicCustomer() throws JMSException, IOException {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"tcp://192.168.1.20:61616"
);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("huaYuanBaoBao");
MessageConsumer consumer = session.createConsumer(topic);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try{
TextMessage textMessage = (TextMessage) message;
String text = null;
//取出消息的内容
text= textMessage.getText();
System.out.println(text);
}catch (Exception e){
e.printStackTrace();
}
}
});
System.out.println("消费端03");
System.in.read();
//关闭资源
connection.close();
consumer.close();
session.close();
}
}
和Spring整合:
spring-amq.xml:
tcp://192.168.1.20:61616
AMQSenderServiceImpl:
package com.demo.spring;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
@Service
public class AMQSenderServiceImpl {
private static final Logger logger = LoggerFactory.getLogger(AMQSenderServiceImpl.class);
@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate;
//目的地队列的明证,我们要向这个队列发送消息
@Resource(name = "destinationQueue")
private Destination destination;
//向特定的队列发送消息
public void sendMsg(final User user) {
// final String msg = JSON.toJSONString(mqParamDto);
user.setEmail("[email protected]");
user.setPassword("123456");
user.setPhone("123456");
user.setSex("M");
user.setUsername("javaceshi");
try {
logger.info("将要向队列{}发送的消息msg:{}", destination, user);
jmsTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
// return session.createObjectMessage(user);
return session.createTextMessage("2019/1/18message");
}
});
} catch (Exception ex) {
logger.error("向队列{}发送消息失败,消息为:{}", destination, user);
}
}
}
AMQReceiverServiceImpl:
package com.demo.spring;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
@Service
public class AMQReceiverServiceImpl {
private static final Logger logger = LoggerFactory.getLogger(AMQSenderServiceImpl.class);
@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate;
//目的地队列的明证,我们要向这个队列接收消息
@Resource(name = "destinationQueue")
private Destination destination;
//向特定的队列接收消息
public void receiverMsg(final User user) {
//
try {
Object object = jmsTemplate.receive(destination);
User msg = (User) object;
System.out.println(msg);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
测试类:App
package com.demo.spring;
import com.demo.spring.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 主发送类
*
*/
public class App
{
public static void main( String[] args )
{
final User user = new User();
user.setEmail("[email protected]");
user.setPassword("123456");
user.setPhone("123456");
user.setSex("M");
user.setUsername("javaceshi");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-amq.xml");
AMQSenderServiceImpl sendService = (AMQSenderServiceImpl)context.getBean("amqSenderService");
sendService.sendMsg(user);
// sendService.send(user);
System.out.println("send successfully, please visit http://192.168.1.20:8161/admin to see it");
}
}