Spring---rabbitmq生产者配置

这篇文章是Spring整合Rabbitmq的其中针对生产者的一个简单的配置。如何来实现java向rabbitmq中发送消息

作为一个生产者,我们要配置哪些东西,我认为生产者需要配置的有几个必要的:

<?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.xsd
            http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/rabbit
                http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
	<!-- spring amqp默认的是jackson 的一个插件,目的将生产者生产的数据转换为json存入消息队列,由于Gson的速度快于jackson,这里替换为Gson的一个实现 -->
	<bean id="jsonMessageConverter"
		class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"></bean>
	<!-- 连接服务配置 -->
	<rabbit:connection-factory id="connectionFactory"
		host="127.0.0.1" username="guest" password="guest" port="5672"
		virtual-host="/" />
	<rabbit:admin connection-factory="connectionFactory" />
	
	<!-- spring template声明 -->
	<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"  exchange="wdgexchange" 
	 message-converter="jsonMessageConverter"  routing-key="*"  />
	
	<!-- queue 队列声明 -->
	<rabbit:queue durable="true" auto-delete="false" exclusive="false" name="wdgqueue" />
	<!-- exchange queue binging key 绑定 -->
	
	<!-- rabbitmq交换器 -->
	<rabbit:direct-exchange name="wdgexchange" id="wdgexchange"
		durable="true" auto-delete="false">
		<rabbit:bindings>
			<rabbit:binding queue="wdgqueue" key="*" />
		</rabbit:bindings>
	</rabbit:direct-exchange>
	
</beans>

1.jsonMessageCoverter这个bean的配置。

2.链接包括密码,用户名,和主机ip地址,端口号,以及虚拟的virtual-host

3.链接工厂

4.声明模板,模板的声明里面需要有exchange,route-key ,id,connectFactory,message-converter,

5.声明队列

6.交换器

上面是rabbitmq-producer.xml的配置,我们应该如何实现消息的发送:

    public static void main(String[] args) {
    	ApplicationContext context = new ClassPathXmlApplicationContext("rabbitmq-producer.xml");
    	AmqpTemplate amqpTemplate = context.getBean(RabbitTemplate.class); 
    	amqpTemplate.convertAndSend("444");
	}

运行程序就可以实现消息的发送:

扫描二维码关注公众号,回复: 4889419 查看本文章

希望上面rabbitmq生产者的配置对你有所帮助,如果感觉确实有帮助了,可以扫描一下红包哦

                                                                           

猜你喜欢

转载自blog.csdn.net/datouniao1/article/details/84893690