Spring+ActiveMQ学习总结

Spring+ActiveMQ学习总结

实验要求:
1、 1个apache作为负载均衡器、3个以上的tomcat作为web应用服务器
2、 分别采用session sticky和session replication进行配置实验
3、 要求能实现3个及以上tomcat应用服务器节点的负载均衡,并支持节点热拔插
4、编写Web应用程序模拟用户注册时候发送通知邮件的应用场景,用ActiveMQ的Topic方式实现异步的邮件发送和接收消息。

好久没有写博客了,懒懒的。
那么我那么懒的人,为什么要写这篇呢???因为记仇!!!
中间件课程设计花了我起码3-4天的时间了!
这里写图片描述

本学期中间件的课程设计我采用的是软负载均衡:apache+tomcat负载均衡实验+ActiveMQ实现JMS监听队列。

由于我的项目ActiveMQ的注解不生效,所以我尝试了很多种方法!开始本次记仇…..

1.1 maven依赖

<dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>4.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.9.1</version>
        </dependency>
        <dependency>
            <groupId>javax.jms</groupId>
            <artifactId>javax.jms-api</artifactId>
            <version>2.0.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.4.7</version>
        </dependency>

1.2 命名空间引入

<?xml version="1.0" encoding="UTF-8"?>
<!-- 查找最新的schemaLocation 访问 http://www.springframework.org/schema/ -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">

1.3配置applicationContext.xml

<!-- jms配置 -->
    <!-- ActiveMQ 真正产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
    <bean id="activeMqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
        <property name="userName" value="admin"></property>
        <property name="password" value="admin"></property>
    </bean>

    <!-- 配置spring 管理 connectionFactory -->
    <bean id="connectionFactory"
        class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="activeMqConnectionFactory" />
        <property name="sessionCacheSize" value="100" />
    </bean>  

    <!-- 消息处理器 -->
    <bean id="jmsMessageConverter"
        class="org.springframework.jms.support.converter.SimpleMessageConverter" />
    <!-- ====Producer side start==== -->
    <!-- 定义JmsTemplate的Queue类型 -->
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <constructor-arg ref="connectionFactory" />
        <!-- 非pub/sub模型(发布/订阅),即队列模式 -->
        <property name="pubSubDomain" value="false" />
        <property name="messageConverter" ref="jmsMessageConverter"></property>
    </bean>
    <!-- 定义JmsTemplate的Topic类型 -->
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
        <constructor-arg ref="connectionFactory" />
        <!-- pub/sub模型(发布/订阅) -->
        <property name="pubSubDomain" value="true" />
        <property name="messageConverter" ref="jmsMessageConverter"></property>
    </bean>
    <jms:listener-container destination-type="queue"
        container-type="default" connection-factory="connectionFactory"
        acknowledge="auto" concurrency="5-10">
        <jms:listener destination="testqueue" ref="consumerServiceImpl" />
    </jms:listener-container>

1.4 生产者

@Resource
@Component("producer")
public class Producer {
    @Resource(name = "jmsQueueTemplate")
    private JmsTemplate jmsQueueTemplate;// 通过@Qualifier修饰符来注入对应的bean

    public String[] args;
     public void sendMessage(Destination destination, final String msg){ 
         jmsQueueTemplate.convertAndSend(destination, msg);  
         System.out.println(msg);
     }  
}

1.5 消费者

@Component("consumerServiceImpl")
public  class Consumer  implements MessageListener {
    public String[] args;

    public void onMessage(Message message) {    
        TextMessage textMsg = (TextMessage) message;  

        try {
            String msg = textMsg .getText();
            System.out.println("55555"+msg);    
            //调用发送邮件方法
            SendEmail.main(args, msg);
        } catch (Exception e) {

        }
    }
}

1.6 实现
登录项目后新增老师,新增成功后则发送邮件新增成功!
这里写图片描述

这里写图片描述

项目码云地址:https://gitee.com/srain13/keshe/tree/activemq/

下面点名表扬几篇博客。

ActiveMQ学习总结(10)——ActiveMQ采用Spring注解方式发送和监听
https://blog.csdn.net/u012562943/article/details/52993462

Spring整合JMS(二)——三种消息监听器
http://elim.iteye.com/blog/1893676

猜你喜欢

转载自blog.csdn.net/Srain13/article/details/80594880
今日推荐