Example usage for org.springframework.jms.listener DefaultMessageListenerContainer start

List of usage examples for org.springframework.jms.listener DefaultMessageListenerContainer start

Introduction

In this page you can find the example usage for org.springframework.jms.listener DefaultMessageListenerContainer start.

Prototype

@Override
public void start() throws JmsException 

Source Link

Document

Overridden to reset the stop callback, if any.

Usage

From source file:com.jmstoolkit.pipeline.Main.java

/**
 *
 * @param args//from  ww w.ja v a  2 s.co  m
 */
public static void main(final String[] args) {
    try {
        Settings.loadSystemSettings("jndi.properties");
        Settings.loadSystemSettings("app.properties");
    } catch (JTKException ex) {
        System.out.println("Failed to load application settings");
        System.out.println(JTKException.formatException(ex));
        System.exit(1);
    }

    final ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] {
            "/infrastructure-context.xml", "/mdb-context.xml", "/jmx-context.xml", "/logging-context.xml" });
    applicationContext.start();

    final DefaultMessageListenerContainer dmlc = (DefaultMessageListenerContainer) applicationContext
            .getBean("listenerContainer");
    if (dmlc != null) {
        dmlc.start();
        final Pipeline pipeline = (Pipeline) applicationContext.getBean("pipelineService");
        // enable access to the original application context
        pipeline.setApplicationContext(applicationContext);

        // Insure that the Pipeline loads configuration files AFTER
        // the listenerContainer is running.
        while (!dmlc.isRunning()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
            }
        }
        pipeline.loadPlugins();
        pipeline.sendProperties();
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
        }
    }
}

From source file:com.jmstoolkit.pipeline.Pipeline.java

/**
 *
 * @param args the command line arguments
 *//*from w  w  w  . j  av  a2  s . c  om*/
public static void main(final String[] args) {
    // Have to use System.out as logging is configured by Spring
    // application context.
    try {
        Settings.loadSystemSettings("jndi.properties");
        Settings.loadSystemSettings("app.properties");
    } catch (JTKException ex) {
        System.out.println("Failed to load application settings");
        System.out.println(JTKException.formatException(ex));
        System.exit(1);
    }

    final ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] {
            "/logging-context.xml", "/infrastructure-context.xml", "/mdb-context.xml", "/jmx-context.xml" });
    applicationContext.start();

    final DefaultMessageListenerContainer dmlc = (DefaultMessageListenerContainer) applicationContext
            .getBean("listenerContainer");
    if (dmlc != null) {
        dmlc.start();
        final Pipeline pipeline = (Pipeline) applicationContext.getBean("pipelineService");
        // enable access to the original application context
        pipeline.setApplicationContext(applicationContext);

        // Insure that the Pipeline loads configuration files AFTER
        // the listenerContainer is running.
        while (!dmlc.isRunning()) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
            }
        }
        pipeline.loadPlugins();
        pipeline.sendProperties();

        // Keep thread alive
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
        }
    }
}

From source file:siia.jms.MessageListenerContainerDemo.java

public static void main(String[] args) {
    // establish common resources
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
    Destination queue = new ActiveMQQueue("siia.queue");
    // setup and start listener container
    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setDestination(queue);// w  w  w. ja v a2  s. c o m
    container.setMessageListener(new MessageListener() {
        public void onMessage(Message message) {
            try {
                if (!(message instanceof TextMessage)) {
                    throw new IllegalArgumentException("expected TextMessage");
                }
                System.out.println("received: " + ((TextMessage) message).getText());
            } catch (JMSException e) {
                throw new RuntimeException(e);
            }
        }
    });
    container.afterPropertiesSet();
    container.start();
    // send Message
    JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
    jmsTemplate.setDefaultDestination(queue);
    jmsTemplate.convertAndSend("Hello World");
}

From source file:ch.algotrader.event.TopicEventDumper.java

public static void main(String... args) throws Exception {

    SingleConnectionFactory jmsActiveMQFactory = new SingleConnectionFactory(
            new ActiveMQConnectionFactory("tcp://localhost:61616"));
    jmsActiveMQFactory.afterPropertiesSet();
    jmsActiveMQFactory.resetConnection();

    ActiveMQTopic topic = new ActiveMQTopic(">");

    DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
    messageListenerContainer.setDestination(topic);
    messageListenerContainer.setConnectionFactory(jmsActiveMQFactory);
    messageListenerContainer.setSubscriptionShared(false);
    messageListenerContainer.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);

    messageListenerContainer.setMessageListener((MessageListener) message -> {
        try {/* w  w  w  .j  a  v  a 2 s  .com*/
            Destination destination = message.getJMSDestination();
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                System.out.println(destination + " -> " + textMessage.getText());
            } else if (message instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) message;
                byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
                bytesMessage.readBytes(bytes);
                System.out.println(destination + " -> " + new String(bytes, Charsets.UTF_8));
            }
        } catch (JMSException ex) {
            throw new UnrecoverableCoreException(ex);
        }
    });

    messageListenerContainer.initialize();
    messageListenerContainer.start();

    System.out.println("Dumping messages from all topics");

    Runtime.getRuntime().addShutdownHook(new Thread(messageListenerContainer::stop));

    Thread.sleep(Long.MAX_VALUE);
}

From source file:terrastore.event.impl.ActiveMQEventBus.java

private void createConsumer(Event event) {
    String queueName = TERRASTORE_QUEUE_PREFIX + event.getBucket();
    if (!consumers.containsKey(queueName)) {
        stateLock.lock();//from   w w  w . j ava  2 s .c  o m
        try {
            if (!shutdown && !consumers.containsKey(queueName)) {
                DefaultMessageListenerContainer consumer = new DefaultMessageListenerContainer();
                consumer.setConnectionFactory(jmsConnectionFactory);
                consumer.setSessionTransacted(true);
                consumer.setMessageListener(new EventProcessor(eventListeners, actionExecutor));
                consumer.setDestinationName(queueName);
                consumer.start();
                consumer.initialize();
                consumers.put(queueName, consumer);
            }
        } finally {
            stateLock.unlock();
        }
    }
}

From source file:terrastore.event.impl.ActiveMQEventBus.java

private void initConsumers(String broker) throws Exception {
    ActiveMQConnection connection = null;
    DestinationSource destinations = null;
    try {/*from  w w w. j a  v  a 2  s  . c  o  m*/
        connection = ActiveMQConnection.makeConnection(broker);
        connection.start();
        //
        destinations = connection.getDestinationSource();
        destinations.start();
        //
        Set<ActiveMQQueue> queues = destinations.getQueues();
        for (ActiveMQQueue queue : queues) {
            if (queue.getQueueName().startsWith(TERRASTORE_QUEUE_PREFIX)) {
                LOG.info("Listening to queue: {}", queue.getQueueName());
                DefaultMessageListenerContainer consumer = new DefaultMessageListenerContainer();
                consumer.setConnectionFactory(jmsConnectionFactory);
                consumer.setSessionTransacted(true);
                consumer.setMessageListener(new EventProcessor(eventListeners, actionExecutor));
                consumer.setDestinationName(queue.getQueueName());
                consumer.start();
                consumer.initialize();
                consumers.put(queue.getQueueName(), consumer);
            }
        }
    } finally {
        if (destinations != null) {
            destinations.stop();
        }
        if (connection != null) {
            connection.stop();
            connection.close();
        }
    }
}

From source file:ch.algotrader.service.SubscriptionServiceImpl.java

private void updateMessageSelector(final DefaultMessageListenerContainer genericMessageListenerContainer,
        String messageSelector) {

    genericMessageListenerContainer.setMessageSelector(messageSelector);

    // restart the container (must do this in a separate thread to prevent dead-locks)
    (new Thread() {
        @Override//from  ww w . jav a 2s.co m
        public void run() {
            genericMessageListenerContainer.stop();
            genericMessageListenerContainer.shutdown();
            genericMessageListenerContainer.start();
            genericMessageListenerContainer.initialize();
        }
    }).start();
}

From source file:org.springframework.integration.jms.request_reply.RequestReplyScenariosWithTempReplyQueuesTests.java

@Test
public void messageCorrelationBasedOnRequestCorrelationIdTimedOutFirstReply() throws Exception {
    ActiveMqTestUtils.prepare();/*from  w  ww . ja  v  a  2 s.c om*/
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "producer-temp-reply-consumers.xml", this.getClass());
    RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);

    final Destination requestDestination = context.getBean("siOutQueue", Destination.class);

    DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
    dmlc.setConnectionFactory(connectionFactory);
    dmlc.setDestination(requestDestination);
    dmlc.setMessageListener((SessionAwareMessageListener<Message>) (message, session) -> {
        Destination replyTo = null;
        try {
            replyTo = message.getJMSReplyTo();
        } catch (Exception e1) {
            fail();
        }
        String requestPayload = (String) extractPayload(message);
        if (requestPayload.equals("foo")) {
            try {
                Thread.sleep(6000);
            } catch (Exception e2) {
                /*ignore*/ }
        }
        try {
            TextMessage replyMessage = session.createTextMessage();
            replyMessage.setText(requestPayload);
            replyMessage.setJMSCorrelationID(message.getJMSMessageID());
            MessageProducer producer = session.createProducer(replyTo);
            producer.send(replyMessage);
        } catch (Exception e3) {
            // ignore. the test will fail
        }
    });
    dmlc.afterPropertiesSet();
    dmlc.start();

    try {
        gateway.exchange(new GenericMessage<String>("foo"));
    } catch (Exception e) {
        // ignore
    }
    Thread.sleep(1000);
    try {
        assertEquals("bar", gateway.exchange(new GenericMessage<String>("bar")).getPayload());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    context.close();
}