前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >rabbitMQ-simple 简单模式

rabbitMQ-simple 简单模式

作者头像
用户5927264
发布2019-08-01 10:21:26
1900
发布2019-08-01 10:21:26
举报
文章被收录于专栏:OSChina

1 导入依赖

代码语言:javascript
复制
<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 工具类

代码语言:javascript
复制
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 生产者

代码语言:javascript
复制
/**
	 * 生产者
	 * @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 消费者

代码语言:javascript
复制
/**
	 * 消费者
	 * @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);
		}
	}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档