Spring AMQP provides org.springframework.amqp.core.AmqpTemplate to send and receive messages. AMQP template implementation supports sending and receiving POJOs instead javax.jms.Message instance. They also provide a way to customize the layout object for MessageConverter. Spring and JMS users will find the new AmqpTemplate JmsTemplate similarity between.
The following code fragment shows how to use Spring AMQP and RabbitMQ combined handle synchronization messages. RabbitMQ is VMware's products, and is the official Spring AMQP default used in the example AMQP implementation.
<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" xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <rabbit:connection-factory id="connectionFactory"/> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/> <rabbit:admin connection-factory="connectionFactory"/> <rabbit:queue name="helloworld.queue"/> </beans>
the Spring configuration xml references ConnectionFactory, created a RabbitTemplate to access the message broker, created a RabbitAdmin to manage the exchange, query and binding, and finally created a queue. The following Java code fragment for sending and receiving messages.
Producer.java import org.springframework.amqp.core.AmqpTemplate; ... AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class); amqpTemplate.convertAndSend("helloworld.queue", "Hello World");
Consumer.java import org.springframework.amqp.core.AmqpTemplate; ... AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class); System.out.println(amqpTemplate.receiveAndConvert("helloworld.queue"));
To get started, download the Spring AMQP for Java or Spring AMQP for. NET . Spring AMQP for Java artifacts are also located SpringSource Maven repository and Maven Central in. Its dependence is spring-amqp, spring-core and spring-context. If the message broker using RabbitMQ, then still introduce spring-rabbit. You can also try located on GitHub Spring AMQP sample . These examples are based on Maven projects, need to use RabbitMQ with Erlang . Please read the sample code with the release of readme.md file for complete build commands.
for Cloud Foundry users, VMware also release < / a> the RabbitMQ on Cloud Foundry free public Beta. You can connect using AMQP RabbitMQ on Cloud Foundry, through these sample can help you get started quickly . For Spring Integration users, version 2.1 M1 provides AMQP support.
In addition to VMware's RabbitMQ, other AMQP-based message-oriented middleware also Apache Qpid , Red Hat Enterprise MRG and StormMQ (hosting). iMatrix the OpenAMQ is no longer supported, so it did not become an option.
To learn more about Spring AMQPFor more information, please see Spring AMQP Reference for Java with Spring AMQP Reference for. NET Reference page.
view the original English text: Spring AMQP 1.0 GA
没有评论:
发表评论