<div>
</div>
1,Rabbitmq的java端API使用:
详情请参考:http://www.blogjava.net/qbna350816/archive/2016/06/04/430771.html
2,提供一个生产者设备模拟(30个生产者)
package cn.edu.pojo;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
public class Porduct implements Runnable{
private Connection con;
private String queueName;
private static Gson gson = new Gson();
private String IP;
private String routekey;
Porduct(Connection con,String qname,String routekey,String ip){
this.con=con;
this.queueName=qname;
this.IP=ip;
this.routekey=routekey;
}
public void run() {
Channel chan = null;
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
chan = con.createChannel();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true){
try {
SjEntity now = new SjEntity();
now.setIP(IP);
now.setNetName("eth0");
now.setTime(date.format(new Date()));
//申明交换机时各个参数的意思:交换机名称 交换机类型 是否持久化 通道关闭后是否删除
chan.exchangeDeclare("Exchange1", "topic",true,false,null);
//申明消息队列时各个参数含义:消息队列名称 是否持久化 是否具有独占性 是否连接断开时删除 其它设置的一个map
chan.queueDeclare(queueName, true, false, false, null);
//申明发送数据时各个参数含义:交换机名称 Message的routeKey Message的自身属性设置(是否Message持久化) 消息的详细内容
chan.basicPublish("Exchange1", routekey, MessageProperties.PERSISTENT_TEXT_PLAIN, gson.toJson(now).getBytes());
//chan.basicp
System.out.println(IP+"----设备发送数据完毕 时间:"+new java.util.Date());
Thread.sleep(10000); //10秒发一次
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
线程的启动类
package cn.edu.pojo;
import java.io.IOException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Test {
public static void main(String[] args) throws IOException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.0.80");
factory.setUsername("rabbitmq");
factory.setPassword("rabbitmq123");
factory.setPort(5672);
for(int i =0;i<40;i++){
Porduct p = new Porduct(factory.newConnection(), "First", "haha","192.168.12."+i);
Thread xc = new Thread(p);
xc.start();
}
}
}