Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在RabbitMQ中使用不同的路由密钥将多个消息发布到单个队列?_Rabbitmq - Fatal编程技术网

如何在RabbitMQ中使用不同的路由密钥将多个消息发布到单个队列?

如何在RabbitMQ中使用不同的路由密钥将多个消息发布到单个队列?,rabbitmq,Rabbitmq,这是我的密码: 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.co

这是我的密码:

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
package-pushnotiruntest;
导入com.rabbitmq.client.BuiltinExchangeType;
导入com.rabbitmq.client.Channel;
导入com.rabbitmq.client.Connection;
导入com.rabbitmq.client.ConnectionFactory;
导入java.io.IOException;
导入java.util.concurrent.TimeoutException;
导入java.util.logging.Level;
导入java.util.logging.Logger;
公共类发送扩展线程{
字符串名称=”;
字符串app_type=“”;
私有静态最终字符串交换\u NAME=“主题交换”;
公开募捐
{
ConnectionFactory connFac=新的ConnectionFactory();
connFac.setHost(“localhost”);
试一试{
连接conn=connFac.newConnection();
Channel=conn.createChannel();
channel.ExchangeClare(EXCHANGE\u名称,BuiltinExchangeType.TOPIC);

对于(int j=1;j如果需要两个消费者,则必须使用两个队列。 如果从
交换
队列
您无法在订阅期间决定路由密钥,则绑定将被禁用

您可以将更多路由密钥绑定到同一队列,但不能使用该密钥进行过滤

我想你需要这样的东西:

channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
channel.queueDeclare(QUEUE_NAME_1, true, false, false, null);
channel.queueDeclare(QUEUE_NAME_2, true, false, false, null);
channel.queueBind(QUEUE_NAME_1, EXCHANGE_NAME, "my.rk.1");
channel.queueBind(QUEUE_NAME_2, EXCHANGE_NAME, "my.rk.2");
channel_consumer_1.basicConsume(QUEUE_NAME_1, false, new DefaultConsumer(channel_consumer) {...}
....
channel_consumer_2.basicConsume(QUEUE_NAME_2, false, new DefaultConsumer(channel_consumer) {...}

如果我必须向不同的应用程序发送数千条消息(如推送通知),那么为每个应用程序创建队列可能会创建大量队列,这可能很难由服务器处理。我想要的是根据优先级将消息发布到队列中,并将消息返回给各自的消费者(即根据应用程序id)。您必须为设备创建一个队列。您可以解释一下,您知道如何使用不同的绑定密钥将多个消费者绑定到单个队列吗?正如我所说:您可以使用不同的路由密钥绑定队列,而不是绑定消费者