xml mq与Spring整合
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- AOP中强制使用CGLIB代理
<aop:aspectj-autoproxy proxy-target-class="true"/>
-->
<!-- 扫包-->
<context:component-scan base-package="cn.buba.mq.spring.service"/>
<!--
1)连接工厂
2)创建MQ连接池
3)目的地
4)生产者
5)消费者
6)监听器
-->
<!-- 配置连接工厂 -->
<bean id="factory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
<!-- 是否支持异步 -->
<property name="sendAcksAsync" value="true"></property>
</bean>
<!-- 配置jms的连接池,跟JDBC连接池一个道理,用完了之后放回连接池,我们自己写程序不必要创建连接和关闭连接,spring帮我们做 -->
<bean id="factoryPool" class="org.apache.activemq.pool.PooledConnectionFactory">
<!-- 连接池依赖于工厂,它通过工厂创建连接,然后把这些连接放到池子里 -->
<property name="connectionFactory" ref="factory"></property>
<property name="maxConnections" value="30"></property>
</bean>
<!-- 目的地,这里指定的是Queue,也就是基于队列的 -->
<bean id="first" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0">
<value>first</value>
</constructor-arg>
</bean>
<!--
配置生产者,这里使用的是spring包装好的生成者,通过它屏蔽了很多api操作,例如开启连接、关闭连接、commit,rollback...等。
它必须依赖连接池、目的地.
JmsTemplate屏蔽了所有jms的产品的细节,无论是activemq,rabitmq,kafka,roketmq...都一样用它
-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="factoryPool"></property>
<property name="defaultDestinationName" value="first"></property>
<!-- 是不是发布订阅模式,如果上面的目的地是queue这里就是false -->
<property name="pubSubDomain" value="false"></property>
<!-- 是否启用事务,一旦启用则spring帮我们commit/rollback -->
<property name="sessionTransacted" value="true"></property>
<!-- 一个开关,详情可以点进去看 -->
<property name="explicitQosEnabled" value="true" />
<!-- 是否持久化,跟"timeToLive"不矛盾 -->
<property name="deliveryPersistent" value="true"></property>
<!-- 发送消息时如果消息在某个时间段没有处理掉,则从持久化库中删除该信息,谨慎设置,或者根据业务情况设置,以免消费者处理效率太低造成积压后被删除 -->
<property name="timeToLive" value="100000" />
<!-- 消息确认方式,1代表自动确认 -->
<property name="sessionAcknowledgeMode" value="1" />
</bean>
<!--
配置消费者,跟生产者一样,也是spring帮我们封装的,还可以使用SimpleMessageListenerContainer等
-->
<bean id="defaultMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="factoryPool"></property>
<property name="destinationName" value="first"></property>
<property name="pubSubDomain" value="false"></property>
<property name="messageListener" ref="mylistener"></property>
<property name="sessionTransacted" value="true"></property>
</bean>
<!-- 配置监听器,同时它也是业务处理类 -->
<bean id="mylistener" class="cn.buba.mq.spring.listener.JMSLinstener"></bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 该文件功能:将shiro集成到ssm工程 -->
<display-name>activemq</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 配置spring开始 -->
<!--
声明spring配置文件的位置
contextConfigLocation不能更改
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--
spring提供的一个监听器,它监控application对象的创建,换一句话说,它监控者tomcat的启动
其作用:
第一,该监听器读取<context-param>的参数即spring配置文件的路径;
第二,用读到的路径创建sprigIOC容器并将容器放入application对象中。
备注:通过以上两步springIOC容器就伴随着项目整个生命周期,也就是我们创建的
bean伴随着整个生命周期,所以我们才可以在需的地方注入对象。
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring结束 -->
<!-- 配置springmvc开始 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>charactor</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charactor</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置springmvc结束 -->
</web-app>
生产者
package cn.buba.mq.queue.second;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Producer {
public static void main(String[] args) {
//所需要的对象
ConnectionFactory factory=null;
Connection connection=null;
Session session=null;
Destination destination=null;
MessageProducer producer=null;
Message message=null;
String brokerURL="tcp://zhanglikun:61616";
//生成一个消息,并发送
try{
factory=new ActiveMQConnectionFactory(brokerURL);
connection=factory.createConnection();
//启动connection
connection.start();
//创建session
session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
//创建目的地,下面程序创建了一个queue类型的并且名称为first的目的地
destination=session.createQueue("second");
//创建“一个指向first目的地的”生成者
producer=session.createProducer(destination);
//消息
for(int i =0 ;i<1;i++){
message=session.createTextMessage("生成的第"+i+"条消息");
//发送消息
producer.send(message);
}
//提交事务
session.commit();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
监听者
package cn.buba.mq.queue.second;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class JMSListener implements MessageListener {
/* *
* 这是一个覆盖方法
*/
public void onMessage(Message message) {
TextMessage m=(TextMessage)message;
String mess=null;
try{
mess=m.getText();
}catch(JMSException e){
e.printStackTrace();
}
System.out.println(mess);
}
}
消费者
package cn.buba.mq.queue.second;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Consumer {
public static void main(String[] args) {
//所需要的对象
ConnectionFactory factory=null;
Connection connection=null;
Session session=null;
Destination destination=null;
MessageConsumer consumer=null;
TextMessage message=null;
String brokerURL="tcp://zhanglikun:61616";
//创建消费者,并消费信息
try{
factory=new ActiveMQConnectionFactory(brokerURL);
connection=factory.createConnection();
connection.start();
session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建消费者
destination=session.createQueue("second");
consumer = session.createConsumer(destination);
consumer.setMessageListener(new JMSListener());
}catch(Exception e){
e.printStackTrace();
}
}
}