Spring Boot中没有Apache Qpid处理程序问题的消息

时间:2016-02-25 13:30:24

标签: java spring spring-boot qpid

我正在尝试使用Jms Qpid客户端通过Spring Boot应用程序使用Apache Qpid。我可以配置它,但当我尝试从队列接收消息时,记录器正在打印:

Dispatcher(918480905)Received a message(878303980)[1] from queue 1 )without a handler - rejecting(requeue)...

这是我的代码:

JmsConfiguration.java

@Configuration
public class JmsConfiguration {

    @Primary
    @Bean
    public Context createContext()
    {
        Properties properties=new Properties();
        System.setProperty("IMMEDIATE_PREFETCH", "true");

        Context context=null;
        try {
            properties.load(this.getClass().getResourceAsStream("application.properties"));
            context = new InitialContext(properties);
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return context;
    }

    @Primary
    @Bean
    public ConnectionFactory createConnectionFactory(Context context)
    {
        ConnectionFactory connectionFactory=null;
        try {
            connectionFactory = (ConnectionFactory) context.lookup("qpidConnectionFactory");
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connectionFactory;
    }

    @Primary
    @Bean
    public Connection jmsConnection(ConnectionFactory connectionFactory) throws Exception
    {
        Connection connection = connectionFactory.createConnection();
        connection.start();
        return connection;
    }

    @Primary
    @Bean
    public Queue jmsQueue(Context context) throws Exception
    {
        Queue queue = (Queue) context.lookup("myqueue");
        return queue;
    }
}

application.properties

java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory
connectionfactory.qpidConnectionFactory = amqp://guest:guest@clientid/?brokerlist='tcp://localhost:5672?maxprefetch='0''
queue.myqueue = queue1

ScheduledTask.java 它只是间隔运行发送和接收消息。

@Component
public class ScheduledTasks
{
    Connection connection;
    Queue queue;

    @Autowired
    public ScheduledTasks(Connection connection, Queue queue) {
        this.connection=connection;
        this.queue=queue;
    }

    MessageListener messageListener = new MessageListener() {
        @Override
        public void onMessage(Message message) {
            System.out.println("Received id is------>");
            System.out.println(message);
        }
    };

    @Scheduled(fixedDelay = 2000)
    public void sendMessage() throws Exception
    {
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        Message message=session.createTextMessage();
        MessageProducer messageProducer=session.createProducer(queue);
        message.setStringProperty("value", "BOOM");
        messageProducer.send(message);
        session.commit();
        messageProducer.close();
        //connection.close();
        System.out.println("---------------Message Sent");
    }

    //@JmsListener(destination="queue1")
    @Scheduled(initialDelay=5000, fixedDelay = 5000)
    public void receiveMessage() throws Exception
    {
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        MessageConsumer messageConsumer = session.createConsumer(queue);
//        if(messageConsumer.getMessageListener()==null)
//          messageConsumer.setMessageListener(messageListener);
        Message message = messageConsumer.receive(3000);
        if(message!=null)
            System.out.println("----------------->"+message.getStringProperty("value"));
        session.commit();
        messageConsumer.close();
        //connection.close();
        System.out.println("--------------->Got Message");
    }
}

1 个答案:

答案 0 :(得分:0)

您创建了一个实现MessageListener的实例,但您没有对其执行任何操作。

在Spring中,您应该使用spring-jms中的DefaultMessageListenerContainerSimpleMessageListenerContainer,并在JmsConfiguration类中将其创建为Spring Bean。设置连接详细信息(ConnectionFactoryQueuesessionTransacted等)后,您还需要设置JMS MessageListener实施类。