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

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

Introduction

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

Prototype

public void setMessageListener(@Nullable Object messageListener) 

Source Link

Document

Set the message listener implementation to register.

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 w  w.  j  a v a 2 s.  co  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 {//from www .ja  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:org.nebulaframework.util.jms.JMSRemotingSupport.java

/**
 * Remote enables a given object as a remote JMS Service
 * through a given Service Interface./*  www. j a  v  a2  s . c  o  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:uk.gov.nationalarchives.discovery.taxonomy.batch.config.DeleteMsgConfiguration.java

@Bean
DefaultMessageListenerContainer deleteContainer(MessageListenerAdapter deleteListenerAdapter,
        ConnectionFactory connectionFactory) {
    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setMessageListener(deleteListenerAdapter);
    container.setConnectionFactory(connectionFactory);
    container.setDestinationName(deleteDocumentsQueueName);
    return container;
}

From source file:uk.gov.nationalarchives.discovery.taxonomy.batch.config.CategorisationMsgConfiguration.java

@Bean
DefaultMessageListenerContainer categorisationContainer(MessageListenerAdapter categorisationListenerAdapter,
        ConnectionFactory connectionFactory) {
    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setMessageListener(categorisationListenerAdapter);
    container.setConnectionFactory(connectionFactory);
    container.setDestinationName(categoriseDocumentsQueueName);
    return container;
}

From source file:org.apigw.monitoring.config.JmsConfig.java

@Bean
DefaultMessageListenerContainer container(MessageListener messageListener,
        ConnectionFactory connectionFactory) {
    log.debug(String.format("Setting up DefaultMessageListenerContainer for destination: %s",
            monitoringDestination));//from www  .ja  va 2s. c  om
    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setMessageListener(messageListener);
    container.setConnectionFactory(connectionFactory);
    container.setDestinationName(monitoringDestination);
    container.setSessionTransacted(true); //Setting this flag to "true" will use a short local JMS transaction when running outside of a managed transaction.
    return container;
}

From source file:com.jim.im.config.GenericMQConfig.java

/**
 * ???//from w w w. ja  v  a2  s .  co  m
 *
 * @param queueName ??
 * @param rpcServiceExport ??
 * @return
 */
protected MessageListenerContainer buildListenerContainer(JmsInvokerServiceExporter rpcServiceExport,
        String queueName) {
    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setConnectionFactory(jmsConnectionFactory());
    container.setDestinationName(queueName);
    container.setMessageConverter(messageConverter());
    container.setMessageListener(rpcServiceExport);
    container.setConcurrentConsumers(Runtime.getRuntime().availableProcessors());
    return container;
}

From source file:com.jim.im.group.config.GatewayUserRegisterConfig.java

@Bean
public MessageListenerContainer hornetQMessageListenerContainer() {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put(TransportConstants.HOST_PROP_NAME, hornetq_host);
    map.put(TransportConstants.PORT_PROP_NAME, hornetq_port);
    TransportConfiguration transportConfiguration = new TransportConfiguration(
            NettyConnectorFactory.class.getName(), map);

    HornetQConnectionFactory hornetQConnectionFactory = HornetQJMSClient
            .createConnectionFactoryWithoutHA(JMSFactoryType.CF, transportConfiguration);

    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setConnectionFactory((ConnectionFactory) hornetQConnectionFactory);
    container.setDestination(HornetQJMSClient.createTopic(IMConstant.GATEWAY_USERREGISTER_TOPIC));
    container.setPubSubDomain(true);/* w  w  w.java  2  s  .com*/
    container.setPubSubNoLocal(true);
    container.setMessageListener(userRegisterListener);
    container.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
    return container;
}

From source file:com.jim.im.config.GenericMQConfig.java

/**
 * ?????,MQ???/*from w  w  w .  j a v  a2 s . c om*/
 *
 * @param topicName
 * @param messageListener
 * @return
 */
public MessageListenerContainer mqMessageReceiver(String topicName, MessageListener messageListener) {
    DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
    container.setConnectionFactory(jmsConnectionFactory());
    container.setDestinationName(topicName);
    container.setPubSubDomain(true);
    container.setPubSubNoLocal(true);
    container.setMessageListener(messageListener);
    container.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
    return container;
}

From source file:com.rabbitmq.jms.sample.StockConsumer.java

@Bean
public DefaultMessageListenerContainer jmsListener(ConnectionFactory connectionFactory) {

    log.info("connectionFactory => " + connectionFactory);

    DefaultMessageListenerContainer jmsListener = new DefaultMessageListenerContainer();
    jmsListener.setConnectionFactory(connectionFactory);
    jmsListener.setDestinationName("rabbit-trader-channel");
    jmsListener.setPubSubDomain(false);//  w w w.  j a va  2  s  . co  m

    MessageListenerAdapter adapter = new MessageListenerAdapter(new Receiver());
    adapter.setDefaultListenerMethod("receive");

    jmsListener.setMessageListener(adapter);
    return jmsListener;
}