Spring Hot(3)Spring with RabbitMQ

Spring Hot(3)Spring with RabbitMQ

1. Some Concepts
Work Queue
Exchange: direct, topic, headers, fanout

Core Classes —> ConnectionFactory, Connection, Channel, QueueingConsumer
http://wubin850219.iteye.com/blog/1007984

Core Features
spring-amqp, spring-erlang, spring-rabbit
Message
Exchange —> DirectExchange, TopicExchange, FanoutExchange, HeadersExchange
Queue
Binding  —> Exchange, Queue, RoutingKey
AmqpTemplate ——> Send the Message
AmqpAdmin/RabbitAdmin ——> Queue, Exchange, Binding
MessageConverter ——> Convert the message
SimpleMessageListenerContainer ——> Listener

2. Working with Spring Send Message
Reading the samples from official website.
>git clone https://github.com/spring-projects/spring-amqp-samples.git

Import that into our Eclipse
>mvn eclipse:eclipse

3. Configuration on Spring
Here is the dependency configuration
<!-- spring RabbitMQ --><dependencyorg="org/springframework/amqp"  name="spring-rabbit"rev="1.3.4.RELEASE"/><dependencyorg="org/springframework/amqp"  name="spring-amqp"rev="1.3.4.RELEASE"/><dependencyorg="com/rabbitmq"name="amqp-client"rev="3.3.3"/>

Here is the spring configuration
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd             http://www.springframework.org/schema/tx            http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

<rabbit:connection-factory

   id="connectionFactory"      

   addresses="localhost:5672,localhost:5673"

   username="guest" password="guest"/>    

<rabbit:template

   id="amqpTemplate"

   connection-factory="connectionFactory" />    

<rabbit:admin

   id="containerGuest"

   connection-factory="connectionFactory" />   

<rabbit:queue

   name="myqueue1"

   declared-by="containerGuest" />   

<rabbit:queue

   name="myqueue2"

   declared-by="containerGuest" />       

<bean id="messageConverter"                class="org.springframework.amqp.support.converter.SimpleMessageConverter">      </bean>       

<bean id="receiveMessageHandler"    class="com.sillycat.easyspringrabbitmqpublisher.main.ReceiveMessageHandler" />         

<bean id="messageListenerAdapter"  class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">         <constructor-arg ref="receiveMessageHandler" />         

        <property name="defaultListenerMethod" value="handleMessage"></property>          <property name="messageConverter" ref="messageConverter"></property>      </bean>        

<bean id="listenerContainer"          class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">          <constructor-arg ref="connectionFactory" />       

        <property name="queues" ref="myqueue2" />       

        <property name="messageListener" ref="messageListenerAdapter" />     </bean>

</beans>


Sync Send and Receive Message
package com.sillycat.easyspringrabbitmqpublisher.main;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class SendOneMessageApp {

     public static void main(String[] args) {
          ApplicationContext context = new GenericXmlApplicationContext(
                    "classpath:main-context.xml");
          AmqpTemplate template = context.getBean(AmqpTemplate.class);

          // Sync send Message
          template.convertAndSend("myqueue1", "message1");
          System.out.println("Done");
     }
}

package com.sillycat.easyspringrabbitmqpublisher.main;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class ReceiveOneMessageApp {

     public static void main(String[] args) {
          ApplicationContext context = new GenericXmlApplicationContext(
                    "classpath:main-context.xml");
          AmqpTemplate template = context.getBean(AmqpTemplate.class);

          // Sync receive Message
          String foo = (String) template.receiveAndConvert("myqueue1");
          System.out.println("What I get from Sync = " + foo);
     }
}

Async Handle Message
package com.sillycat.easyspringrabbitmqpublisher.main; import java.util.Date; publicclass ReceiveMessageHandler { publicvoid handleMessage(String message){      System.out.println("Received " + message + " at " + new Date()); } }

All the details are in project easyspringrabbitmqpublisher.

Tips
1. Permission Problem
Error Message:
Caused by: com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:338)at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:590)

Solutions:
Visit this page and change what you want.
http://localhost:15672/#/users



References:
http://wubin850219.iteye.com/blog/1002932  Simple Working Queue, Buffering
http://wubin850219.iteye.com/blog/1003840  Working Queue, Split tasks to workers
http://wubin850219.iteye.com/blog/1004921   Publish/Subscribe
http://wubin850219.iteye.com/blog/1004948   Routing
http://wubin850219.iteye.com/blog/1004973   Topics
http://wubin850219.iteye.com/blog/1007984   Core Class

http://sillycat.iteye.com/blog/2066116  Configure the Cluster

http://projects.spring.io/spring-amqp/#quick-start
http://docs.spring.io/spring-amqp/docs/1.3.4.RELEASE/reference/html/quick-tour.html#d4e59
http://wubin850219.iteye.com/blog/1050279   sync 
http://wubin850219.iteye.com/blog/1050328   async

Samples
https://github.com/spring-projects/spring-amqp-samples

User Management
https://www.rabbitmq.com/man/rabbitmqctl.1.man.html

猜你喜欢

转载自sillycat.iteye.com/blog/2085828