`
knight_black_bob
  • 浏览: 858992 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

rabbitmq spring

阅读更多

 

 

demo 下载 :http://download.csdn.net/download/knight_black_bob/9544857

 

 



 

 

 

 

applicationContext-rabbit-consumer.xml

<?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.xsd">

	<context:property-placeholder location="classpath:rabbitmq.properties" />

	<rabbit:connection-factory  id="connectionFactory"
		host="${rabbit.host}" 
		username="${rabbit.username}" 
		password="${rabbit.password}" />
		
	<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
		<constructor-arg ref="connectionFactory" />
	</bean>

	<rabbit:admin connection-factory="connectionFactory" />
	<rabbit:queue id="cqueue"  name="${rabbit.queue.name}"   durable="true" auto-delete="false" exclusive="false" />

	<rabbit:direct-exchange id="cmq-exchange" durable="true"
		auto-delete="false" name="${rabbit.exchange.name}">
		<rabbit:bindings>
			<rabbit:binding queue="cqueue" key="cqueue-key"></rabbit:binding>
		</rabbit:bindings>
	</rabbit:direct-exchange>

	<bean id="listener" class="com.curiousby.core.MessageProcessListener" />
	
	<rabbit:listener-container id="listenerContainer" connection-factory="connectionFactory" >
		<rabbit:listener ref="listener" queues="cqueue" />
	</rabbit:listener-container>
  
	
</beans>

 

applicationContext-rabbit-producer.xml

<?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">

	<context:property-placeholder location="classpath:rabbitmq.properties" />

	<rabbit:connection-factory id="connectionFactory"
		host="${rabbit.host}"
		username="${rabbit.username}" 
		password="${rabbit.password}" />

	<rabbit:admin connection-factory="connectionFactory" />

	<rabbit:queue id="pqueue"  name="${rabbit.queue.name}" />
	 
	<rabbit:direct-exchange id="pmq-exchange" durable="true" auto-delete="false" name="pmq-exchange">
		<rabbit:bindings>
			<rabbit:binding queue="pqueue" key="pqueuekey" />
		</rabbit:bindings>
	</rabbit:direct-exchange>

	<bean id="jsonMessageConverter"
		class="com.curiousby.util.FastJsonMessageConverter"></bean>
	  
	  
	  <rabbit:template 
	        exchange="pmq-exchange" 
	  		id="amqpTemplate"  
	  		connection-factory="connectionFactory"  
	 		message-converter="jsonMessageConverter"/>

</beans>

 

applicationContext.xml

<?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:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
	default-autowire="byName"> 
     
    <context:annotation-config />
	<aop:aspectj-autoproxy /> 
	
	<context:property-placeholder location="classpath:rabbitmq.properties" />

	<context:component-scan base-package="com.curiousby" /> 
	
</beans>

 

package com.curiousby.entity;

public class Message {

	public String msgId;
	public String content;
	public String from;
	public String to;
	public String isValid;
	public String insertTime;
	public String lastUpdateTime;

	
	public Message (){}
	public Message(String msgId, String content, String from, String to, String isValid, String insertTime,
			String lastUpdateTime) { 
		this.msgId = msgId;
		this.content = content;
		this.from = from;
		this.to = to;
		this.isValid = isValid;
		this.insertTime = insertTime;
		this.lastUpdateTime = lastUpdateTime;
	}

	public String getMsgId() {
		return msgId;
	}

	public String getContent() {
		return content;
	}

	public String getFrom() {
		return from;
	}

	public String getTo() {
		return to;
	}

	public String getIsValid() {
		return isValid;
	}

	public String getInsertTime() {
		return insertTime;
	}

	public String getLastUpdateTime() {
		return lastUpdateTime;
	}

	public Message setMsgId(String msgId) {
		this.msgId = msgId;
		return this;
	}

	public Message setContent(String content) {
		this.content = content;
		return this;
	}

	public Message setFrom(String from) {
		this.from = from;
		return this;
	}

	public Message setTo(String to) {
		this.to = to;
		return this;
	}

	public Message setIsValid(String isValid) {
		this.isValid = isValid;
		return this;
	}

	public Message setInsertTime(String insertTime) {
		this.insertTime = insertTime;
		return this;
	}

	public Message setLastUpdateTime(String lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
		return this;
	}

	@Override
	public String toString() {
		return "Message [msgId=" + msgId + ", content=" + content + ", from=" + from + ", to=" + to + ", isValid="
				+ isValid + ", insertTime=" + insertTime + ", lastUpdateTime=" + lastUpdateTime + "]";
	}

	
	
}

 

package com.curiousby.core;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonAnyFormatVisitor;
//com.curiousby.core.MessageProcessListener
public class MessageProcessListener implements MessageListener{

	@Override
	public void onMessage(Message message) { 
		process(message);
	}

	private void  process(Message message){
		if (message != null) {
			byte[] msg = message.getBody();
			System.out.println("===============" + msg.toString());
		}
	}
	
}

 

 

package com.curiousby.core;

import javax.annotation.Resource;

import org.springframework.amqp.core.AmqpTemplate; 
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Repository
public class MessagePush {

	@Resource
	private AmqpTemplate amqpTemplate;
	 
	public void convertAndSend(Object obj) {
	     amqpTemplate.convertAndSend("pqueuekey", obj);
	}	
	 
}

 

 

package com.curiousby.util;

import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.AbstractMessageConverter;
import org.springframework.amqp.support.converter.MessageConversionException;

import com.alibaba.fastjson.JSON; 
//import fe.json.FastJson;



//com.curiousby.util.FastJsonMessageConverter
public class FastJsonMessageConverter  extends AbstractMessageConverter {
    private static Log log = LogFactory.getLog(FastJsonMessageConverter.class);

    public static final String DEFAULT_CHARSET = "UTF-8";

    private volatile String defaultCharset = DEFAULT_CHARSET;
    
    public FastJsonMessageConverter() {
        super(); 
    }
    
    public void setDefaultCharset(String defaultCharset) {
        this.defaultCharset = (defaultCharset != null) ? defaultCharset
                : DEFAULT_CHARSET;
    }
    
    public Object fromMessage(Message message)
            throws MessageConversionException {
        return null;
    }
    
    public <T> T fromMessage(Message message,T t) {
        String json = "";
        try {
            json = new String(message.getBody(),defaultCharset);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return   (T) JSON.parseObject(json, t.getClass());
        		//(T) FastJson.fromJson(json, t.getClass());
    }	
    

    protected Message createMessage(Object objectToConvert,
            MessageProperties messageProperties)
            throws MessageConversionException {
        byte[] bytes = null;
        try {
            String jsonString = JSON.toJSONString(objectToConvert);
            		//FastJson.toJson(objectToConvert);
            bytes = jsonString.getBytes(this.defaultCharset);
        } catch (UnsupportedEncodingException e) {
            throw new MessageConversionException(
                    "Failed to convert Message content", e);
        } 
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
        messageProperties.setContentEncoding(this.defaultCharset);
        if (bytes != null) {
            messageProperties.setContentLength(bytes.length);
        }
        return new Message(bytes, messageProperties);

    }
}

 

 

package com.curiousby;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.curiousby.core.MessagePush;

@Component
@RunWith(SpringJUnit4ClassRunner.class)   
@ContextConfiguration(locations = {"classpath*:applicationContext*.xml"}) 
public class MainStart {

 
	@Autowired
	MessagePush messagePush;
	
	@Test
	public  void testMain() throws InterruptedException{ 
			Thread.sleep(100000000);
	}
}

 

package com.curiousby;

import java.util.UUID;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.curiousby.core.MessagePush;
import com.curiousby.entity.Message;

@Component
@RunWith(SpringJUnit4ClassRunner.class)   
@ContextConfiguration(locations = {"classpath*:applicationContext*.xml"}) 
public class TestProducter {

	@Autowired
	MessagePush messagePush;
	
	@Test
	public  void testMain() throws InterruptedException{ 
		Thread.sleep(10000);
		Message m = new Message();
		m.setContent("baoyou")
		 .setMsgId(UUID.randomUUID().toString().replaceAll("-", ""))
		 .setFrom("1")
		 .setTo("2");
		messagePush.convertAndSend(m);
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

  • 大小: 165.1 KB
0
1
分享到:
评论

相关推荐

    rabbitmq spring demo

    rabbitmq spring rabbitmq spring rabbitmq spring rabbitmq spring http://knight-black-bob.iteye.com/blog/2304089

    java rabbitmq spring springAMQP 代码包 project

    在IT行业中,Java、RabbitMQ、Spring以及Spring AMQP是四个非常重要的技术组件,它们在构建高效、可扩展的企业级应用中起着至关重要的作用。这个“java rabbitmq spring springAMQP 代码包 project”显然是一个综合...

    rabbitmq与spring集成示例demo

    在这个"rabbitmq与spring集成示例demo"中,我们将探讨如何将RabbitMQ集成到基于Spring的应用程序中,以便利用其消息传递能力。以下是一些关键知识点: 1. **Maven集成**:Maven是Java项目管理工具,用于构建、依赖...

    RabbitMq与Spring整合实例

    将RabbitMQ与Spring整合,可以方便地在Spring应用中使用消息队列,实现异步通信和任务调度。 本实例主要介绍如何在Spring应用中集成RabbitMQ,构建一个完整的消息传递系统。首先,你需要确保已经安装了RabbitMQ...

    spring事物和rabbitMQ的例子

    本示例聚焦于Spring的事务管理和RabbitMQ的使用,这都是分布式系统中不可或缺的组件。 首先,让我们深入了解Spring的事务管理。在Java环境中,事务管理是确保数据一致性的重要手段。Spring提供了一种声明式事务管理...

    springboot+rabbitmq项目demo(亲测,可正常运行)

    这里我们引入了`spring-boot-starter-amqp`,它是SpringBoot对RabbitMQ的支持,以及`spring-boot-starter-web`用于提供HTTP服务。 接下来,我们需要配置RabbitMQ。在`application.yml`或`application.properties`...

    介绍Spring Cloud Stream与RabbitMQ集成

    介绍Spring Cloud Stream与RabbitMQ集成的代码示例。Spring Cloud Stream是一个建立在Spring Boot和Spring Integration之上的框架,有助于创建事件驱动或消息驱动的微服务。

    springboot整合rabbitmq,开启手工确认。保证消息100%投递

    在Spring Boot应用中整合RabbitMQ,以确保消息100%投递,是一个关键的实践,特别是对于那些需要高可靠性和数据一致性的系统。RabbitMQ是一个流行的开源消息代理,它遵循Advanced Message Queuing Protocol (AMQP)...

    rabbitmq + spring boot demo 消息确认、持久化、备用交换机、死信交换机等代码

    RabbitMQ作为一款流行的开源消息中间件,广泛应用于Spring Boot项目中。本教程将详细介绍如何在Spring Boot应用中结合RabbitMQ实现消息确认、消息持久化、备用交换机以及死信交换机等功能。 首先,让我们理解这些...

    rabbitmq+spring需要的jar包

    其次,`spring-rabbit-1.4.5.RELEASE.jar`是Spring与RabbitMQ之间的桥梁,它扩展了Spring AMQP,提供了具体的实现细节,如RabbitTemplate,用于发送和接收消息,以及RabbitAdmin,用于管理RabbitMQ的实体,如交换器...

    RabbitMQ与SpringMVC集成

    1. 引入依赖:在项目中添加RabbitMQ的Spring整合依赖,如`spring-amqp`库。 2. 配置RabbitMQ:在Spring的配置文件中,定义连接工厂、信道配置以及RabbitMQ服务器的相关属性。 3. 创建消息模板:使用`RabbitTemplate`...

    rabbitmq-tutorial.zip

    【标题】:“rabbitmq-tutorial.zip”是一个与RabbitMQ相关的教程压缩包,可能是为了演示如何在Spring框架中使用RabbitMQ实现“Hello, World!”的简单应用。 【描述】:“本文件为文章...

    RabbitMQ整合spring示例代码(java maven)

    8. **测试与调试**:在`spring-rabbitmq`目录中,可能包含了单元测试或集成测试代码,用于验证RabbitMQ与Spring的集成是否正确,以及消息发送和接收功能是否正常。 通过以上步骤,我们可以构建一个完整的RabbitMQ与...

    详解Spring Boot 配置多个RabbitMQ

    Spring Boot 配置多个 RabbitMQ Spring Boot 是一个流行的 Java 框架,用于快速构建生产级别的应用程序。RabbitMQ 是一个消息队列中间件,用于实现异步消息处理和队列管理。配置多个 RabbitMQ 实例是为了提高系统的...

    rabbitmq和spring集成

    将RabbitMQ与Spring集成,可以方便地在Spring应用中使用消息队列,实现高效的通信和任务处理。 集成RabbitMQ和Spring主要有以下几个关键步骤: 1. **添加依赖**:在Spring项目的Maven或Gradle配置文件中,引入...

    rabbitmq和spring简单整合

    RabbitMQ作为一款开源的消息中间件,被广泛应用于Java开发环境中,尤其是与Spring框架的整合,使得开发者能够轻松地在应用中实现消息的发送和接收。本文将详细介绍如何在Spring项目中与RabbitMQ进行简单整合。 首先...

    spring整合rabbitmq需要的jar包(spring版本4.2.0)

    当我们将Spring与RabbitMQ整合时,可以利用Spring的IOC(Inversion of Control)和AOP(Aspect-Oriented Programming)特性来优雅地管理消息的生产与消费。 在"spring整合rabbitmq需要的jar包(spring版本4.2.0)...

Global site tag (gtag.js) - Google Analytics