有没有办法在Java RabbitMQ

时间:2018-05-10 06:04:04

标签: java annotations rabbitmq

我在创建动态列表器时遇到了问题:@RabbiListner注释没有提供插入不是常量的属性的方法。但在我的项目中,我只会在动态中获得交换和队列的名称。例如:

@Component
@Profile("!test")
public class AmqpReceiver {

    @Autowired
    private ProcessEngine camunda;

    private String exchnageName;

    private String queueName;

    public AmqpReceiver(ProcessEngine camunda, String exchnageName, String queueName) {
        this.camunda = camunda;
        this.exchnageName = exchnageName;
        this.queueName = queueName;
    }

   /**
    * Consumer for starting business process instance with required params
    */
    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = queueName, durable = "true"),
        exchange = @Exchange(value = exchnageName, type = ExchangeTypes.TOPIC, durable = "true"),
        key = "*"))
    @Transactional
    public void receiveQueue(Message<byte[]> message) {
        String payload = new String(message.getPayload());
        Type type = new TypeToken<HashMap<String, Object>>() {
        }.getType();
        HashMap<String, Object> requestParams = new Gson().fromJson(payload, type);
        ProcessInstance processInstance = camunda.getRuntimeService().startProcessInstanceByKey("processName", requestParams);
    }
}

有没有办法让这项工作?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

是的,有一种方法可以动态地创建一个监听器:

SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames("queueName");
    container.setMessageConverter(jsonMessageConverter());
    container.setMessageListener(new Consumer(container));
    container.start();