2016-08-05 32 views
1
消耗在春季启动数据

我是新来春的RabbitMQ并已经知道如何使用像这样使用消息:需要帮助使用兔注释与AMQP

配置文件:

import org.springframework.amqp.core.AmqpAdmin; 
import org.springframework.amqp.rabbit.connection.ConnectionFactory; 
import org.springframework.amqp.rabbit.core.RabbitAdmin; 
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; 
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; 
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; 
import org.springframework.amqp.support.converter.MessageConverter; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
public class ConsumerConfiguration { 

    private String queueName; 

    @Autowired 
    private ConnectionFactory connectionFactory; 

    public void setQueueName(final String queueName) { 
     this.queueName = queueName; 
    } 

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

    @Bean 
    public MessageConverter messageConverter() { 
     final Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter(); 
     return converter; 
    } 

    @Bean 
    public SimpleMessageListenerContainer listenerContainer() { 
     final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); 
     container.setConnectionFactory(this.connectionFactory); 
     container.setQueueNames(this.queueName); 
     container.setMessageListener(messageListenerAdapter()); 

     return container; 
    } 

    @Bean 
    public MessageListenerAdapter messageListenerAdapter() { 
     return new MessageListenerAdapter(this, messageConverter()); 
    } 

} 

用户档案:

import org.springframework.stereotype.Component; 

import com.vts.cped.basic.TopicNames; 
import com.vts.cped.correlator.data.CorrelatedData; 
import com.vts.cped.main.config.BaseReceiver; 

@Component 
public class Consumer { 

    public void handleMessage(final SomeData data) { 
     System.out.println("Consumer received SomeData with data " + data.getData()); 
    } 
} 

我试图实现与RabbitMQ的注释同样的事情,希望它将使代码更干净。我尝试用下面要做到这一点,但我不能得到它的工作:

import org.springframework.amqp.rabbit.annotation.RabbitListener; 
import org.springframework.stereotype.Service; 

@Service 
public class Receiver { 

    @RabbitListener(queues = "tut.hello") 
    public void process(final SomeData in) { 
    System.out.println(" [x] Received '" + in + "'"); 
    } 

} 

本来我试过这样的事情,但它也没有工作:

import org.springframework.amqp.rabbit.annotation.RabbitHandler; 
import org.springframework.amqp.rabbit.annotation.RabbitListener; 
import org.springframework.stereotype.Service; 

@RabbitListener(queues = "tut.hello") 
@Service 
public class Receiver { 

    @RabbitHandler 
    public void process(final SomeData in) { 
    System.out.println(" [x] Received '" + in + "'"); 
    } 

} 

如果你有秘密酱,请给我一些!感谢您提供任何帮助。

+0

我会假设,在你的配置'queueName'创建豆时总是为空.. – Piotr

回答

1

有文档中声明:

要启用@RabbitListener注解支持添加@EnableRabbit您@Configuration类 之一。

http://docs.spring.io/spring-amqp/docs/1.6.1.RELEASE/reference/html/_reference.html#async-annotation-driven-enable

你也可能还需要一个SimpleRabbitListenerContainerFactory豆注册Jackson2JsonMessageConverter

http://docs.spring.io/spring-amqp/docs/1.6.1.RELEASE/reference/html/_reference.html#async-annotation-conversion

+0

此外,我在下面的示例张贴在这里https://github.com/kop-still/java-test/tree/master/spring-boot-sample-amqp和这里一个nd https://github.com/spring-projects/spring-amqp-samples/tree/master/rabbitmq-tutorials(如果有帮助)。 –

0

我找到了修复,我错过了这一点:

@Bean 
public RabbitTemplate rabbitTemplate() { 
    final RabbitTemplate template = new RabbitTemplate(this.connectionFactory); 
    template.setMessageConverter(messageConverter()); 
    return template; 
} 

总的来说,这是我的配置文件:

import org.springframework.amqp.core.AmqpAdmin; 
import org.springframework.amqp.core.Queue; 
import org.springframework.amqp.rabbit.annotation.EnableRabbit; 
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; 
import org.springframework.amqp.rabbit.connection.ConnectionFactory; 
import org.springframework.amqp.rabbit.core.RabbitAdmin; 
import org.springframework.amqp.rabbit.core.RabbitTemplate; 
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; 
import org.springframework.amqp.support.converter.MessageConverter; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 

@EnableRabbit 
@Configuration 
public class CommonConfig { 

    @Autowired 
    private ConnectionFactory connectionFactory; 

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

    @Bean 
    public MessageConverter messageConverter() { 
     final Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter(); 
     return converter; 
    } 

    @Bean 
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() { 
     final SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); 
     factory.setConnectionFactory(this.connectionFactory); 
     factory.setMessageConverter(messageConverter()); 
     factory.setConcurrentConsumers(3); 
     factory.setMaxConcurrentConsumers(10); 
     return factory; 
    } 

    @Bean 
    public RabbitTemplate rabbitTemplate() { 
     final RabbitTemplate template = new RabbitTemplate(this.connectionFactory); 
     template.setMessageConverter(messageConverter()); 
     return template; 
    } 

    @Bean 
    public Queue helloQueue() { 
     return new Queue("tut.hello"); 
    } 

    @Bean 
    public Sender sender() { 
     return new Sender(); 
    } 

    @Bean 
    public Receiver receiver() { 
     return new Receiver(); 
    } 
} 
+0

我发现我需要做以下工作:(1)阅读AMQP规范并更好地理解它(2)完整阅读Spring AMQP文档并(3)查看http://www.rabbitmq.com上的资料/how.html。我以为我可以做一些教程并理解它。 –