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

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

Introduction

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

Prototype

@Override
public void afterPropertiesSet() 

Source Link

Document

Delegates to #validateConfiguration() and #initialize() .

Usage

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);/*from  w ww  .  j  a v a  2s .com*/
    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:org.nebulaframework.util.jms.JMSRemotingSupport.java

/**
 * Remote enables a given object as a remote JMS Service
 * through a given Service Interface.//from   w ww  .ja  v  a 2  s  .co  m
 * 
 * @param <T> Type of Service Interface
 * @param cf JMS Connection Factory
 * @param queueName Name of JMS Queue used for communication
 * @param service Service object to be remote enabled
 * @param serviceClass Service Interface Class
 * @return Message Listener Container
 */
public static <T> DefaultMessageListenerContainer createService(ConnectionFactory cf, String queueName,
        T service, Class<T> serviceClass) {

    ActiveMQQueue queue = new ActiveMQQueue(queueName);

    // Export Service
    JmsInvokerServiceExporter exporter = new JmsInvokerServiceExporter();
    exporter.setServiceInterface(serviceClass);
    exporter.setService(service);
    exporter.afterPropertiesSet();

    // Set up MessageListenerContainer
    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setConnectionFactory(cf);
    container.setDestination(queue);
    container.setMessageListener(exporter);
    container.afterPropertiesSet();

    return container;
}

From source file:org.nebulaframework.grid.cluster.manager.services.jobs.splitaggregate.AggregatorServiceImpl.java

/**
 * Internal method which handles the aggregator start-up for 
 * a given {@code GridJobProfile}.//from  w  w w  .  j  a  va2s . c o  m
 * <p>
 * Creates and starts a {@code ResultCollector} instance for
 * the given {@code GridJob}, and also creates necessary JMS message
 * handling infrastructure.
 * 
 * @param profile {@code GridJobProfile} jobProfile
 */
protected void doStartAggregator(GridJobProfile profile, SplitAggregateJobManager manager) {

    // Create Message Listener Adapter and Result Collector for Job
    MessageListenerAdapter adapter = new MessageListenerAdapter();
    adapter.setDefaultListenerMethod("onResult");

    // Create JMS Message Listener Container
    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setDestinationName(JMSNamingSupport.getResultQueueName(profile.getJobId()));
    container.setMessageListener(adapter);

    //Create results collector and set it as Execution Manager
    ResultCollector collector = new ResultCollector(profile, manager, container);
    profile.setExecutionManager(manager);
    manager.addResultCollector(profile.getJobId(), collector);

    // Initialize Adapter and Container
    adapter.setDelegate(collector);
    container.afterPropertiesSet();
}

From source file:org.apache.servicemix.jms.endpoints.JmsProviderEndpoint.java

/**
 * Create the message listener container to receive response messages.
 * /*from   w w w .  java2  s  . c  o m*/
 * @return
 */
protected AbstractMessageListenerContainer createListenerContainer() {
    DefaultMessageListenerContainer cont;
    if (isJms102()) {
        cont = new DefaultMessageListenerContainer102();
    } else {
        cont = new DefaultMessageListenerContainer();
    }
    cont.setConnectionFactory(getConnectionFactory());
    cont.setDestination(getReplyDestination());
    cont.setPubSubDomain(isPubSubDomain());
    cont.setPubSubNoLocal(isPubSubNoLocal());
    cont.setMessageListener(new MessageListener() {
        public void onMessage(Message message) {
            JmsProviderEndpoint.this.onMessage(message);
        }
    });
    cont.setAutoStartup(false);
    cont.afterPropertiesSet();
    return cont;
}

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

@Test
public void messageCorrelationBasedOnRequestCorrelationIdTimedOutFirstReply() throws Exception {
    ActiveMqTestUtils.prepare();//from w w w.  ja  va 2s . 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();
}