記錄自用
一 安裝
A win系統:
1 下載安裝 http://www.erlang.org/downloads Erlang語言環境
2 配置環境變量 ERLANG_HOME=D:\Program Files\erl9.3
以及Path,在原來的值後面加上“;%ERLANG_HOME%\bin”
3 下載安裝 http://www.rabbitmq.com/install-windows-manual.html
4 配置環境變量RABBITMQ_HOEM=D:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.5
以及Path,在原來的值後面加上“;%RABBITMQ_HOME%\sbin”
5 啓用web管理,在sbin下cmd執行 rabbitmq-plugins enable rabbitmq_management,可在瀏覽器訪問http://localhost:15672,賬戶guest/guest
二 例子
A 手寫
maven
<dependencies>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
生產者
package com;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.security.Provider;
import java.util.concurrent.TimeoutException;
public class Test1 {
static String qName="queue1";
public static void main(String[] args) {
ConnectionFactory f = new ConnectionFactory();
f.setHost("localhost");
Connection c = null;
Channel ch = null;
try {
c = f.newConnection();
ch = c.createChannel();
//創建隊列 (隊列聲明) 參數:隊列名,是否持久化,是否通道獨佔,不使用時自動刪除,其他參數
ch.queueDeclare(qName,false,false,false,null);
String msg[] = {"a1","b","c1","d","e"};
for(int i=0;i<msg.length;i++){
/*try {
new Thread().sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
//插入隊列 參數:路由器,路由鍵,基本屬性,消息體
ch.basicPublish("",qName,null,msg[i].getBytes());
System.out.println("in "+msg[i]);
}
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}finally {
if(ch!=null){
try {
ch.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
if(c!=null){
try {
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
消費者
package com;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Test2 {
public static void main(String[] args) {
ConnectionFactory f = new ConnectionFactory();
f.setHost("localhost");
Connection c;
Channel ch;
try {
c = f.newConnection();
ch = c.createChannel();
//隊列聲明,與生產者相同
ch.queueDeclare(Test1.qName,false,false,false,null);
Consumer co = new DefaultConsumer(ch){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body,"UTF-8");
System.out.println("out "+msg);
}
};
//消費 參數:隊列名,自動確認,接受消息者
ch.basicConsume(Test1.qName,true,co);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
//不關閉連接,可持續消費隊列中出現的對象
}
}
B spring
pom
<?xml version="1.0" encoding="UTF-8"?>
<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>t1</groupId>
<artifactId>t1</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 引入amqp依賴,它能很好的支持RabbitMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 引入test依賴,這次需要用到JUnit -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
application.properties
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
server.port=8081
server.address=127.0.0.1
Spring啓動類Application.java
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@ServletComponentScan
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
控制層(自便)Controller
package com.controller;
import com.component.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value = "/")
public class IndexContro {
@Autowired
private Sender s;
//通過http://localhost:8081/index手動訪問激活
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(){
s.send();
System.out.println("controller");
return "";
}
}
組件,生產者Sender
package com.component;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//生產
@Component
public class Sender {
@Autowired
AmqpTemplate at;
//發送
public void send(){
String content = "qwer";
//隊列名,內容
at.convertAndSend("hh",content);
System.out.println("Sender "+content);
}
}
組件,消費者Receiver
package com.component;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//消費
@Component
@RabbitListener(queues = "hh")//隊列名
public class Receiver {
//消費
@RabbitHandler
public void receiver(String str){
System.out.println("receiver "+str);
}
}
隊列配置Config
package com.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
@Bean
public Queue helloQueue(){
return new Queue("hh");
}
}
目錄結構
啓動後訪問 http://127.0.0.1:8081/index ,可在控制檯查看效果
參考: