spring整合ActiveMQ实现邮件发送

通过mq中间件完成异步发送邮件实现思路:

【1】  本地项目-->【2】activeMQ--->【3】消费者(jms)

说明:

【1】本地项目,创建邮件模板,通过JmsTemple发送给MQ,进入消息队列,是不折不扣的生产者。由于本文讨论的是是运用spring整合activeMQ,需要将jms提供链接的ConnectionFactory交给spring容器管理。

【2】  mq本身嵌套一个jetty服务器,可存储生产者端发送的消息队列。

【3】  消费者端,mq通过messageListener监听器监听并得到消息队列中的消息并对消息做处理。实现异步发送。

总体代码实现:

A.maven引入相关jar

     

    <dependency>

       <groupId>org.springframeworkgroupId>

       <artifactId>spring-coreartifactId>

       <version>${spring.version}version>

    dependency>

    <dependency>

       <groupId>org.springframeworkgroupId>

       <artifactId>spring-webartifactId>

       <version>${spring.version}version>

    dependency>

   

    <dependency>

       <groupId>org.springframeworkgroupId>

       <artifactId>spring-oxmartifactId>

       <version>${spring.version}version>

    dependency>

   

    <dependency>

       <groupId>org.springframeworkgroupId>

       <artifactId>spring-txartifactId>

       <version>${spring.version}version>

    dependency>

   

    <dependency>

       <groupId>org.springframeworkgroupId>

       <artifactId>spring-jdbcartifactId>

       dependency>

   

    <dependency>

       <groupId>org.springframeworkgroupId>

       <artifactId>spring-webmvcartifactId>

       dependency>

   

    <dependency>

       <groupId>org.springframeworkgroupId>

       <artifactId>spring-aopartifactId>

       <version>${spring.version}version>

    dependency>

   

    <dependency>

       <groupId>org.springframeworkgroupId>

       <artifactId>spring-context-supportartifactId>

    dependency>

 

    <dependency>

       <groupId>org.springframeworkgroupId>

       <artifactId>spring-testartifactId>

    dependency>

 

 

  

  <dependency>

    <groupId>javax.mailgroupId>

    <artifactId>mailartifactId>

  dependency>

 

 

    <dependency>

       <groupId>org.springframeworkgroupId>

       <artifactId>spring-jmsartifactId>

       <version>${spring.version}version>

    dependency>

   

    <dependency>

       <groupId>org.apache.activemqgroupId>

       <artifactId>activemq-allartifactId>

       <version>${activemq.version}version>

    dependency>

   

B、jms链接交给spring管理

    <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">

       <property name="brokerURL" value="tcp://localhost:61616" />

       <property name="clientID" value="api2_producer" />

    bean>

C、生产者配置

   

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">

       <property name="defaultDestination" ref="defaultDestination" />

       <property name="connectionFactory" ref="connectionFactory" />

       <property name="pubSubDomain" value="false" />

      

       <property name="explicitQosEnabled" value="true" />

      

       <property name="deliveryMode" value="1" />

      

       <property name="sessionAcknowledgeMode" value="2" />

    bean>

 

   

    <bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">

      

       <constructor-arg index="0" value="defaultJmsQueue" />

    bean>

D、创建生产者消息转换器

publicclass InnerMessageConverter implements MessageConverter{

    @Override

    public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {

       MapMessage message = session.createMapMessage();

 

       @SuppressWarnings("all")

       Map map = (Map) object;

 

       message.setObject("title", map.get("title"));

       message.setObject("content", map.get("content"));

       message.setObject("to", map.get("to"));

       message.setObject("cc", map.get("cc"));

       message.setObject("type", map.get("type"));

 

       returnmessage;

    }

 

    @Override

    public Object fromMessage(Message message) throws JMSException, MessageConversionException {

       returnmessage;

    }

}

 

E、消费者配置,监听器配置:

    <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">

       <property name="brokerURL" value="${brokerURL}" />

       <property name="clientID" value="${clientId}" />

    bean>

   

   

    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">

      

       <constructor-arg index="0" value="defaultJmsQueue" />

    bean>

 

   

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">

       <property name="defaultDestination" ref="destination" />

       <property name="connectionFactory" ref="connectionFactory" />

       <property name="pubSubDomain" value="false" />

      

       <property name="explicitQosEnabled" value="true" />

      

       <property name="deliveryMode" value="1" />

      

       <property name="sessionAcknowledgeMode" value="2" />

    bean>

   

    <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">

       <property name="connectionFactory" ref="connectionFactory" />

       <property name="destination" ref="destination" />

       <property name="messageListener" ref="messageReceiver" />

      

       <property name="sessionTransacted" value="false" />

    bean>

F、消费者实现MessageListener接口

    publicvoid processMessage(MapMessage message) throws Exception {

       //根据消息类型来判断使用具体的发送消息类

       IMessageSender sender = this.factory.getMessageSender(message.getString(MessageConstant.MessageType));

       if (null == sender) {

           thrownew RuntimeException("Not Support");

       }

       try {

           sender.sendMessage(message);

       } catch (Exception ex) {

           log.error(ex);

           thrownew RuntimeException("Not Support");

       }

    }

你可能感兴趣的:(spring系列,activeMQ)