How can I convert an Object to Json in a Rabbit reply?

I have two applications communicating with each other using rabbit.

I need to send (from app1) an object to a listener (in app2) and after some process (on listener) it answer me with another object, now I am receiving this error: ClassNotFound

I am using this config for rabbit in both applications:

@Configuration
public class RabbitConfiguration {

    public final static String EXCHANGE_NAME = "paymentExchange";

    public final static String EVENT_ROUTING_KEY = "eventRoute";
    public final static String PAYEMNT_ROUTING_KEY = "paymentRoute";

    public final static String QUEUE_EVENT = EXCHANGE_NAME + "." + "event";
    public final static String QUEUE_PAYMENT = EXCHANGE_NAME + "." + "payment";
    public final static String QUEUE_CAPTURE = EXCHANGE_NAME + "." + "capture";

    @Bean
    public List<Declarable> ds() {
        return queues(QUEUE_EVENT, QUEUE_PAYMENT);
    }

    @Autowired
    private ConnectionFactory rabbitConnectionFactory;

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(rabbitConnectionFactory);
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(EXCHANGE_NAME);
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate r = new RabbitTemplate(rabbitConnectionFactory);
        r.setExchange(EXCHANGE_NAME);
        r.setChannelTransacted(false);
        r.setConnectionFactory(rabbitConnectionFactory);
        r.setMessageConverter(jsonMessageConverter());
        return r;
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    private List<Declarable> queues(String... nomes) {
        List<Declarable> result = new ArrayList<>();

        for (int i = 0; i < nomes.length; i++) {
            result.add(newQueue(nomes[i]));
            if (nomes[i].equals(QUEUE_EVENT))
                result.add(makeBindingToQueue(nomes[i], EVENT_ROUTING_KEY));
            else
                result.add(makeBindingToQueue(nomes[i], PAYEMNT_ROUTING_KEY));
        }
        return result;
    }

    private static Binding makeBindingToQueue(String queueName, String route) {
        return new Binding(queueName, DestinationType.QUEUE, EXCHANGE_NAME, route, null);
    }

    private static Queue newQueue(String nome) {
        return new Queue(nome);
    }

}

I send the message using this:

String response = (String) rabbitTemplate.convertSendAndReceive(RabbitConfiguration.EXCHANGE_NAME,
            RabbitConfiguration.PAYEMNT_ROUTING_KEY, domainEvent);

And await for a response using a cast to the object.

This communication is between two different applications using the same rabbit server.

How can I solve this?

I expected rabbit convert the message to a json in the send operation and the same in the reply, so I've created the object to correspond to a json of reply.

Upvotes: 1

Views: 1033

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121552

Show, please, the configuration for the listener. You should be sure that ListenerContainer there is supplied with the Jackson2JsonMessageConverter as well to carry __TypeId__ header back with the reply.

Also see Spring AMQP JSON sample for some help.

Upvotes: 4

Related Questions