Skip to content

convert to different javatype when use @RabbitListener annonation [AMQP-461] #2018

@spring-operator

Description

@spring-operator

Alex Young opened AMQP-461 and commented

I have a problem with @RabbitListener annonation when convert message. I produce a message with User.class in A project and want to consume it in B project with a Map.class, because there is no User.class in B project. But I found the json converter can only convert to User.class before invoking the method and if I use Map to accept it the adapter omit the whole method. That's wired, it's hard to make different project convert to same javabean especially the project use different platforms like nodejs or .NET.

Why not inject to method and convert by param's type just like Spring MVC did?

This is the code:

configuration:

@Configuration
@EnableRabbit
public class RabbitConfig{

	@Autowired CustomJackson mapper;

	Jackson2JsonMessageConverter converter;
	
	@Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
        return connectionFactory;
    }

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

    @Bean
    public RabbitTemplate rabbitTemplate() {
    	RabbitTemplate rt = new RabbitTemplate(connectionFactory());
    	rt.setMessageConverter(converter());
        return rt;
    }
    
    public Jackson2JsonMessageConverter converter() {
    	if(converter == null) converter = new Jackson2JsonMessageConverter();
    	converter.setJsonObjectMapper(mapper);
        return converter;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
    	SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setMessageConverter(converter());
        return factory;
    }
    
    @Bean
    public Queue myQueue() {
       return new Queue("core/user/myquery");
    }


}

producer in project A:

@Service
public class Producer {

	@Autowired RabbitTemplate rt;

	public void sendMessage (){
		User user = new User();
		user.id = "aaa";
		user.name = "yec";
		user.orders = new ArrayList<Order>();
		Order o = new Order();
		o.id = "1";
		o.name = "order1";
		user.orders.add(o);
		rt.convertAndSend("core/user/myquery", user);
	}

}

consumer in project B:

@Component
public class Consumer {

    @RabbitListener(queues = "core/user/myquery")
    public void processOrder(Map<String,String> user) {
    	System.out.println("user:" + user.toString());
    }
}

Affects: 1.4.1

Issue Links:

1 votes, 4 watchers

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions