根据学习的RabbitMQ知识配了一个SpringMVC的实现。这是一个完整的工程,view的部分使用freeMarker,持久化操作是通过mybatis实现。
整个工程的目录结构如下:
src下的相关包的解释:
controller:控制器;
domain:对应数据库的操作对象类;
persistence:mybatis的持久化操作;
util:相关工具类;
msg.bean:消息类,定义及描述消息体;
msg.convert:实现如何转化消息体为可接受的消息类;
msg.process:处理器,负责接受到消息后如何处理;
msg.service:包括sender及listener的实现,用于完成发送和接受消息。
config目录存储相关配置文件。
1、配置
web.xml配置,主要配置spring及spring mvc:
代码语言:javascript复制 <display-name>springRabbit</display-name>
<!-- Spring 容器加载的配置文件 设置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!-- Spring 配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Spring MVC 配置 -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
对于springMVC的配置这里不再赘述,本文主要描述的是RabbitMQ的配置,故此处只详细描述RabbitMQ的配置:
applicationContext-mq.xml配置:
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.2.xsd">
<!-- common configuration start-->
<!-- 连接服务配置 -->
<rabbit:connection-factory id="connectionFactory"
host="localhost" username="guest" password="guest" port="5672" />
<rabbit:admin connection-factory="connectionFactory" />
<!-- queue 队列声明-->
<rabbit:queue id="neilyan" durable="true" auto-delete="false"
exclusive="false" name="neilyan" />
<!-- exchange queue binging key 绑定 -->
<rabbit:direct-exchange name="my-mq-exchange"
durable="true" auto-delete="false" id="my-mq-exchange">
<rabbit:bindings>
<rabbit:binding queue="neilyan" key="neilyan_key" />
</rabbit:bindings>
</rabbit:direct-exchange>
<bean id="jsonMessageConverter" class="com.neilyan.msg.convert.JsonMessageConverter"></bean>
<!-- spring template声明-->
<rabbit:template exchange="my-mq-exchange" id="amqpTemplate"
connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
<!-- amqp template 可根据不同业务分为不同template-->
<rabbit:template id="testTemplate" connection-factory="connectionFactory"
message-converter="jsonMessageConverter" reply-timeout="2000"
routing-key="neilyan_key" exchange="my-mq-exchange" />
<!-- common configuration end-->
<!-- sender start-->
<!-- sender 可根据不同业务分为不同sender-->
<bean id="testSender"
class="com.neilyan.msg.service.impl.AmqpMessageSenderImpl">
<property name="amqpTemplate" ref="testTemplate" />
</bean>
<!-- sender中的一种,独立出来是为了发送添加错误code的信息 -->
<bean id="errorMsgSender"
class="com.neilyan.msg.service.impl.AmqpMessageSenderImpl">
<property name="amqpTemplate" ref="testTemplate" />
</bean>
<!-- sender service -->
<bean id="testSendService"
class="com.neilyan.msg.service.sendservice.impl.TestMessageSendServiceImpl">
<property name="messageSender" ref="testSender" />
</bean>
<!-- sender end-->
<!-- processor 这里可以处理响应的业务类型-->
<bean id="testMessageProcessor" class="com.neilyan.msg.process.UserMessageProcessorImpl">
</bean>
<!-- receiver start-->
<!-- listener -->
<bean id="testMessageListener" class="com.neilyan.msg.service.impl.AmqpMessageListenerImpl">
<property name="messageProcessor" ref="testMessageProcessor" />
<property name="messageConverter" ref="jsonMessageConverter" />
<property name="errorMsgSender" ref="errorMsgSender" />
</bean>
<!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象-->
<rabbit:listener-container connection-factory="connectionFactory" concurrency="2" channel-transacted="false" acknowledge="auto">
<rabbit:listener queues="neilyan" ref="testMessageListener" method="onMessage" />
</rabbit:listener-container>
<!-- receiver end-->
</beans>
因为本工程吧sender和receiver的功能都融合在一起了,所以在配置中可以看到同时有sender和receiver的单独配置,以及公共的配置。单独配置其实是一样的,只需要分成两个工程,代码和配置分开就是了。可参考以下文章:http://my.oschina.net/never/blog/140368
对类及配置的具体的、详细的描述在本文就不描写了,实在是太多了,写起来可能过于详细,但是对于开发过springMVC项目的人来说,看了源码就应该能轻松搞定的。