How does spring in a Web project integrate RABBITMQ? __web

Source: Internet
Author: User
Tags aop rabbitmq
How to install RABBITMQ under Windows. This question has been explained clearly in my last article, the students who are not clear can look at my last article. Install and configure RABBITMQ under Windows.
Next, I'll explain how to work with spring in a Web project to use RABBITMQ.
If MAVEN works, add dependencies to the Pom.xml file: If it wasn't for Maven, you'd have to find a jar bag to join the project.
<!--rabbitmq-->
<dependency>
    <groupId>com.rabbitmq</groupId >
    <artifactid>amqp-client</artifactid>
    <version >3.5.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactid >spring-rabbit</Artifactid>
    <version>1.4.5.release</  Version>
</dependency>

Second, the project new configuration file Spring-rabbit.xml
?XML version= "1.0"encoding= "UTF-8"?> <Beansxmlns= "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" xmlns:TX= "Http://www.springframework.org/schema/tx" xmlns:util= "Http://www.springframework.org/schema/util" XSI: schemalocation= "Http://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit-1.5.xsdHttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsdHttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " /c10>> < Context:p Roperty-placeholderlocation= "Classpath*:/meta-inf/env/*.properties"/> <!--<util:properties id= "AppConfig" location= "Classpath:/meta-inf/env/rabbit.properties" ></util :p roperties>--> <!--Configure Connection-factory to specify Connection Rabbit server parameters--> <Rabbit: Connection-factoryID= "ConnectionFactory"username= "Guest"Password= "Guest" Host= "localhost" Port= "5672" Virtual-host="/"/> <!--virtual-host= "/" is the default virtual machine path--> <!--by specifying the following admin information, Exchange and queue in the current producer will automatically generate--> < on the RABBITMQ serverRabbit: adminconnection-factory= "ConnectionFactory"/> <!--definition Queue--> <Rabbit: QueueID= "Com.mj.test"name= "Com.mj.test"Durable= "true"Auto-delete= "false"Exclusive= "false"/> <!--define direct exchange, bind com.mj.test queue--> <Rabbit:d Irect-exchangename= "Mychange"Durable= "true"Auto-delete= "false"> <Rabbit: Bindings> <Rabbit: BindingQueue= "Com.mj.test"Key= "TestKey"></Rabbit: Binding> </Rabbit: Bindings> </Rabbit:d Irect-exchange> <!--define rabbit template for receiving and sending data--> <Rabbit: TemplateID= "Amqptemplate"connection-factory= "ConnectionFactory"Exchange= "Mychange"/> <BeanID= "Messagereceiver"class= "Com.hp.netpro.rf.web.utils.RabbitMQConsumer"/> <Rabbit: Listener-containerconnection-factory= "ConnectionFactory"> <Rabbit: ListenerQueues= "Com.mj.test"ref= "Messagereceiver"/> </Rabbit: Listener-container> </Beans>

Third, in the project configuration file Applicationcontext.xml Add the following configuration:
?XML version= "1.0"encoding= "UTF-8"?> <Beansxmlns= "Http://www.springframework.org/schema/beans" xmlns:XSI= "Http://www.w3.org/2001/XMLSchema-instance" xmlns: Context= "Http://www.springframework.org/schema/context" xmlns:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" XSI: schemalocation= "Http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdHttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop.xsd "> <ImportResource= "Classpath*:/meta-inf/spring/spring-api-mybatis.xml"/> <ImportResource= "Classpath*:/meta-inf/spring/spring-rabbit.xml"/> <!--<import resource= "/meta-inf/spring/spring-rabbit.xml"/>--> <!--enable Aop--> <AOP: Aspectj-autoproxy/> <!--automatic load beans enable--> < Context: Component-scanBase-package= "Com.hp.netpro.rf.web.utils"/> < Context: Annotation-config/> < Context:p Roperty-placeholderlocation= "Classpath*:/meta-inf/env/*.properties"/> <!--<util:properties id= "AppConfig" location= "Classpath:/meta-inf/env/rabbit.properties" ></util :p roperties>--> <!--activation annotation function--> < Context: spring-configured/> </Beans>

Iv. New Message producer class Rabbitmqproducer.java

package com.hp.netpro.rf.web.utils;
Import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * @Description: message producer
 * @Author:
 
* @ Createtime:
 
*/
@Service
public class Rabbitmqproducer {

      @Resource
     private amqptemplate Amqptemplate ;

     public void SendMessage (Object message) {
          amqptemplate . Convertandsend ( "TestKey" , message) //testkey the key for the queue in the configuration file, indicating which queue is sent.
  

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.