@RabittListener忽略routingKey

问题描述 投票:0回答:1

在制片方我送序列化为JSON字符串多个对象类型。区分的目的,我要送的类名作为路由密钥。

在消费方我尝试使用每个多@RabittListener配置与不同的路由密钥,使得只有对应的类被处理。

但它不工作,与其他路由键消息路由到该方法。

我到底做错了什么?

/**
 * Sends an arbitrary object via the configured queue to a amqp instance. To distinguish the message types on the consumer side, we send the message with
 * the objects class name as a routing key.
 *
 * The object is sent as serialized json object
 *
 * @param obj
 * @throws TurbineException
 */
public void send(Object obj) throws TurbineException {

    if (enabled) {
        String routingKey = obj.getClass().getName();
        String json = serializer.toJson(obj);
        byte[] payload = json.getBytes();
        if (connection == null) {
            throw new TurbineException("Trying to send message to RabbitMQ  but connection is null");
        }
        try (Channel channel = connection.createChannel();) {
            log.info(String.format("Sending data of type %s to queue %s", routingKey, queueName));
            log.info(String.format("Data sent: %s", json));
            channel.exchangeDeclare(EMS_META_EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            channel.queueDeclare(queueName, true, false, false, null);
            channel.queueBind(queueName, EMS_META_EXCHANGE_NAME, routingKey);
            channel.basicPublish(EMS_META_EXCHANGE_NAME, routingKey, null, payload);
        }
        catch (IOException | TimeoutException e) {
            throw new TurbineException(e);
        }
    }
}

@Autowired
private ProfileMetaService profileMetaService;

@RabbitListener(bindings = @QueueBinding(value = @Queue(value = MetaServiceApplication.EMS_META_QUEUE, durable = "true"), exchange = @Exchange(value = MetaServiceApplication.EMS_META_EXCHANGE_NAME), key = "com.xaxis.janus.turbine.modeling.profile.ProfileMeta"))
@Transactional
public void processMessage(Message message) {
    try {
        String msg = new String(message.getBody());
        if (LOG.isDebugEnabled()) {
            LOG.debug("ProfileMeta received: {}", msg);
        }
        ProfileMeta profileMeta = fromJson(msg, ProfileMeta.class);
        profileMetaService.save(profileMeta);
    }
    catch (Exception e) {
        throw new RuntimeException(e);
    }
}
amqp spring-amqp
1个回答
0
投票

的RabbitMQ / AMQP不工作的方式;你需要为每个路由密钥不同的队列。

从基于密钥的队列是被用来路由他们消费的无法“选择”的消息。

该路由的交换来完成的;每个消费者都需要自己的队列。

© www.soinside.com 2019 - 2024. All rights reserved.