rabbitmq的相关处理
rabbitmq的安装(参考网上的安装)
安装完成之后可以启动插件web页面,开启rabbitmq_management插件,在web界面查看和管理RabbitMQ服务
rabbitmq-plugins enable rabbitmq_management ### 访问的页面的默认端口为15672
java链接rabbitmq的xml配置
需要引入的jar
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.7.12.RELEASE</version>
</dependency>
xml的配置如下
<?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:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.7.xsd">
<context:property-placeholder location="classpath:config.properties"/>
<!--配置connection-factory,指定连接rabbit server参数 port默认端口5672 -->
<rabbit:connection-factory id="connectionFactory" host="${rabbit.mq.host}" username="${rabbit.mq.username}"
password="${rabbit.mq.password}" port="${rabbit.mq.port}"/>
<!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
<rabbit:admin id="connectAdmin" connection-factory="connectionFactory"/>
<!-- 消息消费者 begin-->
<!-- 定义消费者实现类 begin-->
<bean id="mdmDataHandleService" class="com.fsl.lcp.mq.service.impl.MdmDataHandleServiceImpl"/>
<!-- 定义消费者实现类 end -->
<!-- 定义消费者,并根据消息类型,绑定实现类 -->
<bean id="rabbitMqReceiver" class="com.fsl.lcp.mq.listener.RabbitMqReceiver">
<property name="serviceBeanMap">
<map>
<entry key="test" value-ref="mdmDataHandleService"/>
</map>
</property>
</bean>
<!--定义queue -->
<rabbit:queue name="${rabbit.mq.queue}" durable="true" auto-delete="false" exclusive="false"
declared-by="connectAdmin"/>
<!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->
<rabbit:listener-container connection-factory="connectionFactory">
<rabbit:listener queues="${rabbit.mq.queue}" ref="rabbitMqReceiver"/>
</rabbit:listener-container>
<!-- 消息消费者 end-->
<!-- 消息生产者 begin-->
<bean class="com.fsl.lcp.mq.producer.RabbitMqProducer"></bean>
<!--定义rabbit template用于数据的发送 -->
<rabbit:template id="rabbitMqTemplate" connection-factory="connectionFactory"/>
<!-- 消息生产者 end-->
</beans>
为了消息的可靠性需要消息确认,消息确认分为发送消息确认和接收消息确认
发送消息失败一般出现异常,如果需要有消息发送成功确认需要在rabbit:connection-factory中加publisher-confirms的值为true,需要返回信息可以有publisher-returens
接收消息分为自动,手动以及不需要三种情况,在listener中增加acknowledge:auto:manual:none。如果是auto则表示在方法不抛出异常情况下自动确认,manual:曾通方法发送确认,none:表示接受后mq中会直接删除。
rabbitmq的持久化通过通道中配置durable和交换器中配置durable
浙公网安备 33010602011771号