spring集成rabbitmq

maven配置


        
        
            org.springframework
            spring-core
            4.1.6.RELEASE
        
        
            org.springframework
            spring-beans
            4.1.6.RELEASE
        
        
            org.springframework
            spring-context
            4.1.6.RELEASE
        
        
        
            org.springframework
            spring-test
            4.1.6.RELEASE
        

        
            junit
            junit
            4.11
            test
        


        
        
            com.rabbitmq
            amqp-client
            3.5.1
        
        
            org.springframework.amqp
            spring-rabbit
            1.4.5.RELEASE
        
        
    

rabbitmq.properties

mq.host=192.168.245.128
mq.username=admin
mq.password=admin123
mq.port=5672
mq.vhost=im_vhost

applicationContext.xml



    
        
        
        
            
                
                classpath:rabbitmq.properties
            
        
    

    
    
    
    
    

    
    

    
    

    
    
        
            
        
    

    

    
    
        
    

QueueListenter.java

package rabbitmq;


import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;

/**
 * Created by alber on 2017/3/25.
 */
public class QueueListenter implements ChannelAwareMessageListener {

    public void onMessage(Message message, Channel channel) throws Exception {
        byte[] bytes=message.getBody();
        System.out.println(message.getBody().toString());
        /*提交*/
        //channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        /*回滚*/
        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
    }
}

BaseTest.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;

/**
 * Created by alber on 2017/3/25.
 */

@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:/applicationContext.xml"})
public class BaseTest {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Test
    public void test() throws IOException {
        byte[] bytes="sdfasdf".getBytes();
        amqpTemplate.convertAndSend("queryOne",bytes);
        System.in.read();
    }
}

你可能感兴趣的:(spring集成rabbitmq)