这是我的代码:
package pushnotiruntest;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Send extends Thread {
String name = "";
String app_type = "";
private static final String EXCHANGE_NAME = "topic_exchange";
public void run()
{
ConnectionFactory connFac = new ConnectionFactory();
connFac.setHost("localhost");
try {
Connection conn = connFac.newConnection();
Channel channel = conn.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
for(int j=1; j<=20000; j++)
{
//randomWait();
String routingKey = j+"."+"update"+"."+app_type;
String msg = name;
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes("UTF-8"));
System.out.println("Sent " + routingKey + " : " + msg + "");
}
channel.close();
conn.close();
} catch (IOException ex) {
Logger.getLogger(Send.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Exception1 :--"+ex);
} catch (TimeoutException ex) {
Logger.getLogger(Send.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Exception 2:--"+ex);
}
}
/*void randomWait()
{
try {
Thread.currentThread().sleep((long)(3*Math.random()));
} catch (InterruptedException x) {
System.out.println("Interrupted!");
}
}*/
public static void main(String[] args) {
// TODO code application logic here
Send test1 = new Send();
test1.name = "Hello ANDROID";
test1.app_type = "ANDROID";
Send test2 = new Send();
test2.name = "Hello IOS";
test2.app_type = "IOS";
Send test3 = new Send();
test3.name = "Hello WINDOWS";
test3.app_type = "WINDOWS";
test1.start();
test2.start();
test3.start();
}
}
//javac -cp amqp-client-4.0.2.jar Send.java Recv.java
//java -cp .;amqp-client-4.0.2.jar;slf4j-api-1.7.21.jar;slf4j-simple-1.7.22.jar Recv
//java -cp .;amqp-client-4.0.2.jar;slf4j-api-1.7.21.jar;slf4j-simple-1.7.22.jar
Send
我正在用Java编写代码(使用的消息代理是RabbitMQ),我希望将生成器发送的消息存储在具有不同路由密钥的单个队列中。
根据不同消费者的模式获取消息 与路由密钥模式匹配。 (我正在使用Topic交换进行模式匹配)。