RABBITMQ Introductory Tutorial II--"SPRING+RABBITMQ Simple Demo

Source: Internet
Author: User
Tags object object rabbitmq
1. Maven Engineering pom Files need to be added to the dependency
<dependency>
		    <groupId>org.springframework.amqp</groupId>
		    <artifactId> spring-rabbit</artifactid>
		    <version>1.6.3.RELEASE</version>
		</dependency>

+spring all dependent on +jackson related dependencies


2. The configuration that Spring-config.xml needs to add is as follows:

 <rabbit:connection-factory id= "ConnectionFactory" host= "127.0.0.1" username= "Wuwh" password= "Ganbare" port= "56 "virtual-host="/"/> <rabbit:admin connection-factory=" ConnectionFactory "/> <!--spring tem  Plate declaration--<rabbit:template exchange= "Amqpexchange" id= "Amqptemplate" connection-factory= "ConnectionFactory" message-converter= "Jsonmessageconverter"/> <!--Message Object JSON conversion class--<bean id= "Jsonmessageconverter" Clas s= "Org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/> <rabbit:queue id= "Test_queue "Name=" Test_queue "durable=" true "auto-delete=" false "exclusive=" false "/> <rabbit:direct-exchange name=" Amqpexchange "durable=" true "auto-delete=" false "id=" Test-mq-exchange "> <rabbit:bindings> <rabbi T:binding queue= "Test_queue" key= "Test_queue_key"/> </rabbit:bindings> </rabbit:direct-exchange> & Lt;bean id= "Queuelistenter" class= "CMs.open.itPlatform.service.rq.listener.QueueListenter "> </bean> <rabbit:listener-container connection-factory= "ConnectionFactory" acknowledge= "Auto" > <rabbit:listener queues= "test_queue" ref= " Queuelistenter "/> </rabbit:listener-container>


3. MQ Producer's Interface:

Package CMS.OPEN.ITPLATFORM.SERVICE.RQ;

Public interface Imqproducer {
    /**
     * Send message to specified queue
     * @param queuekey
     * @param object
    */public void Senddatatoqueue (String Queuekey, Object object);
}


MQ Producer's Impl

Package Cms.open.itPlatform.service.rq.impl;

Import Org.apache.log4j.Logger;
Import org.springframework.amqp.core.AmqpTemplate;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;

Import Cms.open.itPlatform.service.rq.IMQProducer;

@Service public
class Mqproducerimpl implements Imqproducer {

	@Autowired
    private amqptemplate Amqptemplate;
	
    Private final static Logger Logger = Logger.getlogger (mqproducerimpl.class);
    Final String Queue_key = "Test_queue_key";
    /* (Non-javadoc)
     * @see Com.stnts.tita.rm.api.mq.mqproducer#senddatatoqueue (java.lang.String, Java.lang.Object)
     *
    /@Override public
    void Senddatatoqueue (String Queuekey, Object object) {
        try {
            Amqptemplate.convertandsend (Queuekey, object);
        } catch (Exception e) {
            logger.error (e);}}
}

Queue Listener Class

Package Cms.open.itPlatform.service.rq.listener;

Import Org.springframework.amqp.core.Message;
Import Org.springframework.amqp.core.MessageListener;

@Component public
class Queuelistenter implements messagelistener{
	@Override public
    void OnMessage ( Message msg) {
        try{
            System.err.println (New String (Msg.getbody (), "UTF-8"));
        catch (Exception e) {
            e.printstacktrace ();}}
}

Test Class Code:

Package cms.open.itPlatform.service.rq.tt;

Import Java.util.HashMap;
Import Java.util.Map;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.testng.annotations.Test;

Import Cms.open.itPlatform.service.rq.IMQProducer;
Import Cms.open.itPlatform.service.rq.impl.MQProducerImpl;

public class Rqtest {
    final String Queue_key = "Test_queue_key";

    @Test public
    Void Send () {
    	ApplicationContext context = new Classpathxmlapplicationcontext ("classpath:/ Spring-config.xml ");
        Imqproducer mqproducer = Context.getbean (mqproducerimpl.class);
    	map<string,object> msg = new hashmap<string,object> ();
        Msg.put ("Data", "hello,rabbmitmq!");
        Mqproducer.senddatatoqueue (queue_key,msg);
    }
}



After reading the RABBITMQ introductory tutorial one or two, I believe you have been able to build your own rabbitmq-server in the Windows environment and write your first demo


RABBITMQ principles and more, please refer to the blog: http://www.cnblogs.com/shanyou/p/4067250.html

and official website: http://www.rabbitmq.com/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.