生产者:
ActiveMQConnectionFactory类
方法:
setUserName(String userName)
setBrokerURL(String brokerURL)
setPassword(String password)
<!-- 第三方MQ工厂: ConnectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- ActiveMQ Address -->
<property name="brokerURL" value="${activemq.brokerURL}" />
<property name="userName" value="${activemq.userName}"></property>
<property name="password" value="${activemq.password}"></property>
</bean>
----------------------------------------------------------------------------------
PooledConnectionFactory类
方法:
setConnectionFactory(final Object toUse)
setMaxConnections(int maxConnections)
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory" ref="targetConnectionFactory" />
<property name="maxConnections" value="${activemq.pool.maxConnections}" />
</bean>
------------------------------------------------------------------------------------------------
SingleConnectionFactory类
方法:
setTargetConnectionFactory(ConnectionFactory targetConnectionFactory)
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="pooledConnectionFactory" />
</bean>
--------------------------------------------------------------------------------------------------
JmsTemplate类
方法:
setConnectionFactory(ConnectionFactory connectionFactory)
setDefaultDestinationName(String destinationName)
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<!-- 队列模板 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestinationName" value="${activemq.queueName}"></property>
</bean>
----------------------------------------------------------------------------------------------------
使用方法:
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
// TODO 自动生成的方法存根
return session.createTextMessage(msg+System.currentTimeMillis());
}
});
=============================================================================================
消费者:
上面ActiveMQConnectionFactory,PooledConnectionFactory,SingleConnectionFactory,JmsTemplate
比生产者多的类:
ActiveMQQueue
方法:
public ActiveMQQueue(String name) {
super(name);
}
<!--这个是目的地:mailQueue -->
<bean id="mailQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>${activemq.queueName}</value>
</constructor-arg>
</bean>
-----------------------------------------------------------------------------------
<!-- 配置自定义监听:MessageListener -->
<bean id="textMessageListener" class="cn.itcast.mq.MessageListener"></bean>
class MessageListener implements SessionAwareMessageListener<Message>类
方法:
public void onMessage(Message msg, Session session)
------------------------------------------------------------------------------------
DefaultMessageListenerContainer类
方法:
public setConnectionFactory(ConnectionFactory connectionFactory)
public void setDestination(Destination destination)
public void setMessageListener(Object messageListener)
<!-- 将连接工厂、目标对了、自定义监听注入jms模板 -->
<bean id="sessionAwareListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="mailQueue" />
<property name="messageListener" ref="textMessageListener" />
</bean>
spring中xml注入机制
1.普通属性,值为引用上面对象时
<property name="destination" ref="mailQueue" />
2.普通属性,值为基本数据类型
<property name="defaultDestinationName" value="${activemq.queueName}">
3.构造方法
<constructor-arg>
<value>${activemq.queueName}</value>
</constructor-arg>
4.list集合
<property name="allawedPass">
<list>
<value>/user/login.htm</value>
<value>/user/regiest.htm</value>
<value>/user/phonelogin.htm</value>
<value>/user/activity.htm</value>
</list>
</property>
相关文章
- 03SpringMvc_自定义的spring.xml配置文件和逻辑视图名
- Log4j的使用和怎么在spring中集成log4j
- 实例讲解-整合iBATIS和Spring在WEB开发中的应用
- spring引用配置文件的时候classpath:和classpath*:的区别
- Spring加载配置和读取多个Properties文件的讲解
- Spring 对集成测试的支持和单元的最佳实践测试(二)
- Spring 对集成测试的支持和单元的最佳实践测试
- Spring对hibernate配置文件hibernate.cfg.xml的集成,来取代hibernate.cfg.xml的配置
- @Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml
- Spring中IOC和AOP的深入讲解