rabbitMQ-simple 简单模式

 

1 导入依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.shi.rabbitmq</groupId>
  <artifactId>rabbitmq-01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  
  <dependencies>
		<!-- RabbitMQ的客户端 -->
		<dependency>
		    <groupId>com.rabbitmq</groupId>
		    <artifactId>amqp-client</artifactId>
		    <version>3.4.1</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3.2</version>
		</dependency>
		
		<dependency>
	        <groupId>org.springframework.amqp</groupId>
	        <artifactId>spring-rabbit</artifactId>
	        <version>1.4.0.RELEASE</version>
	    </dependency>
	    
	    <dependency>
		    <groupId>junit</groupId>
		    <artifactId>junit</artifactId>
		    <version>4.12</version>
		    <scope>test</scope>
		</dependency>
	</dependencies>
  <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

 2  工具类

package com.shi.util;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 创建工厂类--rabbitMq
 * @author SHF
 * @version 创建时间:2018年7月3日  上午9:49:57
 */
public class RabbitMqUtils {
	
	private static String host = "127.0.0.1";
	private static Integer port = 5672;
	private static String userName = "shiye";
	private static String pwd = "shiye";
	private static String vhost = "/test";
	
	public static Connection getConnection() throws IOException, TimeoutException{
		
		//1 定义链接工厂
		ConnectionFactory factory = new ConnectionFactory();
		
		//2 设置服务器地址,端口,账户信息,用户名,密码,vhost
		factory.setHost(host);
		factory.setPort(port);
		factory.setVirtualHost(vhost);
		factory.setUsername(userName);
		factory.setPassword(pwd);
		
		//3 通过工厂获取链接
		Connection connection = factory.newConnection();
		return connection;
	}

}

3 生产者

/**
	 * 生产者
	 * @author SHF
	 * @version 创建时间:2018年7月3日  上午10:08:57
	 * @throws TimeoutException 
	 * @throws IOException 
	 */
	@Test
	public void producer() throws IOException, TimeoutException {
		
		//1  获取链接以及mq通道
		Connection connection = RabbitMqUtils.getConnection();
		Channel channel = connection.createChannel();
		
		//2 申明队列
		DeclareOk queueOK = channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		
		//3 添加消息内容
		String message = "Hello World; 施爷";
		channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
		System.out.println( "sent: '" + message + "'");
		
		//关闭通道和连接
		channel.close();
		connection.close();
	}
	

 4  消费者

/**
	 * 消费者
	 * @author SHF
	 * @version 创建时间:2018年7月3日  上午11:03:25
	 *  @throws Exception
	 */
	@Test
	public void Consum() throws Exception{
		//1 获取链接及mq通道,
		Connection connection = RabbitMqUtils.getConnection();
		Channel channel = connection.createChannel();
		
		//2 申明队列(如果存在就不创建)
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		
		//3 定义队列的消费者
		QueueingConsumer cunusumer =  new QueueingConsumer(channel);
		//4 监听队列
		channel.basicConsume(QUEUE_NAME, true,cunusumer);
		
		//5 获取消息
		while(true) {
			Delivery delivery = cunusumer.nextDelivery();
			String message  = new String(delivery.getBody());
			System.out.println(" received: " + message);
		}
	}

 

转载于:https://my.oschina.net/u/3677987/blog/1839255

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值