标签:时间 work tco 响应时间 user enc note cat core
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> </dependency> <!-- ActiveMQ 和 Spring 整合配置文件标签处理 jar包依赖 --> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> </dependency> <!-- Spring-JMS 插件相关 jar 包依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency>
|
<!-- amq:connectionFactory 是 bean 标签的子标签,会在 spring 容器中创建一个 bean 对象.可以为对象命名. 类似: <bean id="" class="ActiveMQConnectionFactory"></bean>--> <amq:connectionFactory brokerURL="tcp://192.168.70.151:61616" userName="admin" password="admin" id="amqConnectionFactory"/> <!-- spring 管理 JMS 相关代码的时候,必须依赖 jms 标签库. spring-jms 提供的标签库. --> <!-- 定义 Spring-JMS 中的连接工厂对象CachingConnectionFactory - spring 框架提供的连接工厂对象. 不能真正的访问 MOM 容器. 类似一个工厂的代理对象. 需要提供一个真实工厂,实现 MOM 容器的连接访问.--> <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactoryBean"> <property name="connectionFactory" ref="amqConnectionFactory"></property> <property name="maxConnections" value="10"></property> </bean> <!-- 配置有缓存的 ConnectionFactory,session 的缓存大小可定制。 --> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <property name="targetConnectionFactory" ref="pooledConnectionFactory"></property> <property name="sessionCacheSize" value="3"></property> </bean> <!-- JmsTemplate 配置 --> <bean id="template" class="org.springframework.jms.core.JmsTemplate"> <!-- 给定连接工厂, 必须是 spring 创建的连接工厂. --> <property name="connectionFactory" ref="connectionFactory"></property> <!-- 可选 - 默认目的地命名 --> <property name="defaultDestinationName" value="test-spring"></property> </bean>
|
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <property name="targetConnectionFactory" ref="amqConnectionFactory"></property> <property name="sessionCacheSize" value="3"></property> </bean> <!-- 注册监听器 --> <jms:listener-container acknowledge="auto" container-type="default" destination-type="queue" connection-factory="connectionFactory" > <!-- 在监听器容器中注册某监听器对象.destination - 设置目的地命名ref - 指定监听器对象--> <jms:listener destination="test-spring" ref="myListener"/> </jms:listener-container>
|
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.9.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-jms-pool</artifactId> <version>5.9.0</version> </dependency>
|
@Service public class UserServiceImpl implements UserService { @Autowired private JmsTemplate jmsTemplate; @Override public void addUser(final Users user) { //发送消息 this.jmsTemplate.send(new MessageCreator(){ @Override public Message createMessage(Session session) throws JMSException { Message message =session.createObjectMessage(user); return message; } }); } }
|
@Component(value="myListener") public class MyMessageListener implements MessageListener{ @Autowired private UserService userService; @Override public void onMessage(Message message) { //处理消息 ObjectMessage objMessage =(ObjectMessage)message; Users user=null; try { user = (Users)objMessage.getObject(); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.userService.showUser(user); } }
|
标签:时间 work tco 响应时间 user enc note cat core
原文地址:https://www.cnblogs.com/sxshe/p/12164924.html