當前位置: 首頁>>代碼示例>>Java>>正文


Java MessageListener類代碼示例

本文整理匯總了Java中javax.jms.MessageListener的典型用法代碼示例。如果您正苦於以下問題:Java MessageListener類的具體用法?Java MessageListener怎麽用?Java MessageListener使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


MessageListener類屬於javax.jms包,在下文中一共展示了MessageListener類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: on_SUBSCRIBE

import javax.jms.MessageListener; //導入依賴的package包/類
@Test
public void on_SUBSCRIBE() throws JMSException {
	final Session session = mock(Session.class);
	ReflectionUtil.set(this.connection, "session", session);
	final Destination destination = mock(Destination.class);
	when(session.toDestination("/dest")).thenReturn(destination);
	when(session.getConnection()).thenReturn(this.connection);
	final MessageConsumer consumer = mock(MessageConsumer.class);
	when(session.createConsumer(eq(destination), any(String.class))).thenReturn(consumer);

	this.connection.on(new Message("ABC123", Frame.subscribe("1", "/dest").build()));

	verify(this.log).info("Message received from client. [sessionId={},command={}]", "ABC123", Command.SUBSCRIBE);
	verify(session).toDestination("/dest");
	verify(session).getConnection();
	verify(session).createConsumer(destination, "session IS NULL OR session = 'ABC123'");
	verify(consumer).setMessageListener(any(MessageListener.class));
	verifyNoMoreInteractions(session, destination, consumer);
}
 
開發者ID:dansiviter,項目名稱:cito,代碼行數:20,代碼來源:ConnectionTest.java

示例2: setupJmsMessageContainerFullConfig

import javax.jms.MessageListener; //導入依賴的package包/類
@Test
public void setupJmsMessageContainerFullConfig() {
	DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
	MessageListener messageListener = new MessageListenerAdapter();
	SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
	endpoint.setDestination("myQueue");
	endpoint.setSelector("foo = 'bar'");
	endpoint.setSubscription("mySubscription");
	endpoint.setConcurrency("5-10");
	endpoint.setMessageListener(messageListener);

	endpoint.setupListenerContainer(container);
	assertEquals("myQueue", container.getDestinationName());
	assertEquals("foo = 'bar'", container.getMessageSelector());
	assertEquals("mySubscription", container.getSubscriptionName());
	assertEquals(5, container.getConcurrentConsumers());
	assertEquals(10, container.getMaxConcurrentConsumers());
	assertEquals(messageListener, container.getMessageListener());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:20,代碼來源:JmsListenerEndpointTests.java

示例3: createSimpleContainer

import javax.jms.MessageListener; //導入依賴的package包/類
@Test
public void createSimpleContainer() {
	SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
	setDefaultJmsConfig(factory);
	SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();

	MessageListener messageListener = new MessageListenerAdapter();
	endpoint.setMessageListener(messageListener);
	endpoint.setDestination("myQueue");

	SimpleMessageListenerContainer container = factory.createListenerContainer(endpoint);

	assertDefaultJmsConfig(container);
	assertEquals(messageListener, container.getMessageListener());
	assertEquals("myQueue", container.getDestinationName());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:17,代碼來源:JmsListenerContainerFactoryTests.java

示例4: createJmsContainerFullConfig

import javax.jms.MessageListener; //導入依賴的package包/類
@Test
public void createJmsContainerFullConfig() {
	DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
	setDefaultJmsConfig(factory);
	factory.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);
	factory.setConcurrency("3-10");
	factory.setMaxMessagesPerTask(5);

	SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
	MessageListener messageListener = new MessageListenerAdapter();
	endpoint.setMessageListener(messageListener);
	endpoint.setDestination("myQueue");
	DefaultMessageListenerContainer container = factory.createListenerContainer(endpoint);

	assertDefaultJmsConfig(container);
	assertEquals(DefaultMessageListenerContainer.CACHE_CONSUMER, container.getCacheLevel());
	assertEquals(3, container.getConcurrentConsumers());
	assertEquals(10, container.getMaxConcurrentConsumers());
	assertEquals(5, container.getMaxMessagesPerTask());

	assertEquals(messageListener, container.getMessageListener());
	assertEquals("myQueue", container.getDestinationName());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:24,代碼來源:JmsListenerContainerFactoryTests.java

示例5: createJcaContainerFullConfig

import javax.jms.MessageListener; //導入依賴的package包/類
@Test
public void createJcaContainerFullConfig() {
	DefaultJcaListenerContainerFactory factory = new DefaultJcaListenerContainerFactory();
	setDefaultJcaConfig(factory);
	factory.setConcurrency("10");

	SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
	MessageListener messageListener = new MessageListenerAdapter();
	endpoint.setMessageListener(messageListener);
	endpoint.setDestination("myQueue");
	JmsMessageEndpointManager container = factory.createListenerContainer(endpoint);

	assertDefaultJcaConfig(container);
	assertEquals(10, container.getActivationSpecConfig().getMaxConcurrency());
	assertEquals(messageListener, container.getMessageListener());
	assertEquals("myQueue", container.getActivationSpecConfig().getDestinationName());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:18,代碼來源:JmsListenerContainerFactoryTests.java

示例6: createSubscriber

import javax.jms.MessageListener; //導入依賴的package包/類
protected void createSubscriber(TopicSession topicSession, Topic topic) throws JMSException {
    // Create subscriber
    MessageConsumer subscriber = topicSession.createConsumer(topic);

    // Attach message listener to subscriber
    subscriber.setMessageListener(new MessageListener() {
        public void onMessage(javax.jms.Message message) {
            try {
                // Process the message
                processMessage(message);
            } catch (Exception ex) {
                // Error
                logger.error("Error", ex);
            }
        }
    });
}
 
開發者ID:nuagenetworks,項目名稱:java-bambou,代碼行數:18,代碼來源:RestPushCenterJms.java

示例7: setupBus

import javax.jms.MessageListener; //導入依賴的package包/類
public void setupBus(MessageListener msgLis) {
	try {
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL);
		connection = connectionFactory.createConnection();

		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		destination = session.createTopic("OPCUAMilo");
		//Setup producer
		producer = session.createProducer(destination);
		
		//setup consumer
		consumer = session.createConsumer(destination);
		connection.start();
		consumer.setMessageListener(msgLis);
		
	} catch (JMSException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}
 
開發者ID:ashfaqfarooqui,項目名稱:OPCUA-AMQ,代碼行數:23,代碼來源:AMQBus.java

示例8: messageListener

import javax.jms.MessageListener; //導入依賴的package包/類
@Bean
public DefaultMessageListenerContainer messageListener() {
	DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
	container.setConnectionFactory(this.connectionFactory);
	container.setDestinationName("testQueue");
	container.setMessageListener(new MessageListener() {
		@Override
		public void onMessage(Message message) {
			try {
				System.out.println(message.getBody(Object.class));
			}
			catch (JMSException ex) {
				ex.printStackTrace();
			}
		}
	});
	return container;
}
 
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:19,代碼來源:SampleHornetQApplication.java

示例9: receiveMessage

import javax.jms.MessageListener; //導入依賴的package包/類
public static void receiveMessage(final MessageListener listener, final String brokerHost, final String brokerPort, final String topic) throws JMSException {
	// Create a ConnectionFactory
	final ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(String.format("tcp://%s:%s", brokerHost, brokerPort));

	// Create a Connection
	final Connection connection = connectionFactory.createConnection();
	connection.start();

	// Create a Session
	final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

	// Create the destination (Topic or Queue)
	final Destination destination = session.createQueue(topic);

	// register MessageListener
	final MessageConsumer consumer = session.createConsumer(destination);
	consumer.setMessageListener(listener);
}
 
開發者ID:bptlab,項目名稱:Unicorn,代碼行數:19,代碼來源:JMSProvider.java

示例10: receiveMessage

import javax.jms.MessageListener; //導入依賴的package包/類
public static String receiveMessage(MessageListener listener, String brokerHost, String brokerPort, String topic) throws JMSException {
	// Create a ConnectionFactory
	ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(String.format("tcp://%s:%s", brokerHost, brokerPort));

	// Create a Connection
	Connection connection = connectionFactory.createConnection();
	connection.start();

	// Create a Session
	Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

	// Create the destination (Topic or Queue)
	Destination destination = session.createQueue(topic);

	// register MessageListener
	MessageConsumer consumer = session.createConsumer(destination);
	Message message = consumer.receive();
	if (message instanceof TextMessage) {
		return ((TextMessage) message).getText();
	} else {
		return null;
	}
}
 
開發者ID:bptlab,項目名稱:Unicorn,代碼行數:24,代碼來源:JMSProvider.java

示例11: createConsumer

import javax.jms.MessageListener; //導入依賴的package包/類
/**
 * Creates a {@link MessageConsumerResources} with a dedicated
 * {@link Session} required for transacted and InOut consumers.
 */
private MessageConsumerResources createConsumer() throws Exception {
    MessageConsumerResources answer;
    Connection conn = getConnectionResource().borrowConnection();
    try {
        Session session = conn.createSession(isTransacted(), isTransacted() ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE);
        Destination destination = getEndpoint().getDestinationCreationStrategy().createDestination(session, getDestinationName(), isTopic());
        MessageConsumer messageConsumer = JmsObjectFactory.createMessageConsumer(session, destination, getMessageSelector(), isTopic(), getDurableSubscriptionId());
        MessageListener handler = createMessageHandler(session);
        messageConsumer.setMessageListener(handler);

        answer = new MessageConsumerResources(session, messageConsumer);
    } catch (Exception e) {
        log.error("Unable to create the MessageConsumer", e);
        throw e;
    } finally {
        getConnectionResource().returnConnection(conn);
    }
    return answer;
}
 
開發者ID:HydAu,項目名稱:Camel,代碼行數:24,代碼來源:SjmsConsumer.java

示例12: setMessageListener

import javax.jms.MessageListener; //導入依賴的package包/類
/**
 * Set message listener
 *
 * @param listener The listener
 * @throws JMSException Thrown if an error occurs
 */
@Override
public void setMessageListener(final MessageListener listener) throws JMSException {
   session.lock();
   try {
      checkState();
      session.checkStrict();
      if (listener == null) {
         consumer.setMessageListener(null);
      } else {
         consumer.setMessageListener(wrapMessageListener(listener));
      }
   } finally {
      session.unlock();
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:22,代碼來源:ActiveMQRAMessageConsumer.java

示例13: setMessageListener

import javax.jms.MessageListener; //導入依賴的package包/類
/**
 * Sets the message consumer's <CODE>MessageListener</CODE>.
 * <P>
 * Setting the message listener to null is the equivalent of unsetting the
 * message listener for the message consumer.
 * <P>
 * The effect of calling <CODE>MessageConsumer.setMessageListener</CODE>
 * while messages are being consumed by an existing listener or the consumer
 * is being used to consume messages synchronously is undefined.
 *
 * @param listener the listener to which the messages are to be delivered
 * @throws JMSException if the JMS provider fails to receive the next
 *                 message due to some internal error.
 * @see javax.jms.MessageConsumer#getMessageListener
 */
@Override
public void setMessageListener(MessageListener listener) throws JMSException {
    checkClosed();
    if (info.getPrefetchSize() == 0) {
        throw new JMSException("Illegal prefetch size of zero. This setting is not supported for asynchronous consumers please set a value of at least 1");
    }
    if (listener != null) {
        boolean wasRunning = session.isRunning();
        if (wasRunning) {
            session.stop();
        }

        this.messageListener.set(listener);
        session.redispatch(this, unconsumedMessages);

        if (wasRunning) {
            session.start();
        }
    } else {
        this.messageListener.set(null);
    }
}
 
開發者ID:DiamondLightSource,項目名稱:daq-eclipse,代碼行數:38,代碼來源:ActiveMQMessageConsumer.java

示例14: receiveMessagesFromQueue

import javax.jms.MessageListener; //導入依賴的package包/類
/**
 * To receive a message from a queue.
 *
 * @throws JMSException         JMS Exception.
 * @throws InterruptedException Interrupted exception while waiting in between messages.
 */
public void receiveMessagesFromQueue() throws JMSException, InterruptedException {
    QueueConnection queueConn = (QueueConnection) connectionFactory.createConnection();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = queueSession.createQueue(JMSTestConstants.QUEUE_NAME_1);
    MessageConsumer queueReceiver = queueSession.createConsumer(destination);
    MessageListener listener = message -> {
        try {
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                logger.info("Message text received : " + (textMessage.getText()));
            }
        } catch (JMSException e) {
            logger.info("JMS exception occurred.");
        }
    };
    queueReceiver.setMessageListener(listener);
    queueConn.start();
}
 
開發者ID:wso2,項目名稱:carbon-transports,代碼行數:25,代碼來源:JMSServer.java

示例15: setupQueueListener

import javax.jms.MessageListener; //導入依賴的package包/類
/**
 * Create A Consumer for the given Queue and set the given
 * {@link MessageListener}.
 *
 * @param destination The destination of the {@link Queue}
 * @param listener The {@link MessageListener} to set
 * @return true means failure
 */
public synchronized boolean setupQueueListener(final String destination, final MessageListener listener) {
    this.checkAllowedState(State.SESSION_ACTIVE);
    final Destination dest = new ActiveMQQueue(destination);
    MessageConsumer consumer;
    try {
        consumer = this.session.createConsumer(dest);
        consumer.setMessageListener(listener);
    }
    catch (final JMSException ex) {
        LOGGER.error("Problems during setup of QueueListener.", ex);
        this.state = State.FAILURE;
        return true;
    }
    return false;
}
 
開發者ID:MSG134,項目名稱:IVCT_Framework,代碼行數:24,代碼來源:PropertyBasedClientSetup.java


注:本文中的javax.jms.MessageListener類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。