activeMQ和spring集成的配置文件讲解

时间:2021-11-09 20:31:42
生产者:
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>