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


Java MessageConsumer類代碼示例

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


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

示例1: receiveAndRespondWithMessageIdAsCorrelationId

import javax.jms.MessageConsumer; //導入依賴的package包/類
public void receiveAndRespondWithMessageIdAsCorrelationId(ConnectionFactory connectionFactory, String queueName) throws JMSException {
    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(queueName));
    final javax.jms.Message inMessage = consumer.receive();

    String requestMessageId = inMessage.getJMSMessageID();
    LOG.debug("Received message " + requestMessageId);
    final TextMessage replyMessage = session.createTextMessage("Result");
    replyMessage.setJMSCorrelationID(inMessage.getJMSMessageID());
    final MessageProducer producer = session.createProducer(inMessage.getJMSReplyTo());
    LOG.debug("Sending reply to " + inMessage.getJMSReplyTo());
    producer.send(replyMessage);

    producer.close();
    consumer.close();
    session.close();
    connection.close();
}
 
開發者ID:messaginghub,項目名稱:pooled-jms,代碼行數:20,代碼來源:PooledConnectionTempQueueTest.java

示例2: startSyncReceiveThread

import javax.jms.MessageConsumer; //導入依賴的package包/類
private void startSyncReceiveThread(final MessageConsumer messageConsumer, final long receiveTimeout) {
	syncReceiveThread = new SyncReceiveThread() {
		@Override
		public void run() {
			log.info("start listen to the " + typeStr + "[" + queue.getName() + "].");
			while (running) {
				try {
					Message message = messageConsumer.receive(receiveTimeout);
					processMessage(message);
				} catch (Throwable e) {
					// 如果是關閉的時候,可能會報InterruptedException
					log.error("listen to the [" + queue.getName() + "] error.", e);
				}
			}
			closed = true;
		}
	};
	syncReceiveThread.start();
}
 
開發者ID:xsonorg,項目名稱:tangyuan2,代碼行數:20,代碼來源:ActiveMqReceiver.java

示例3: createTopicConsumer

import javax.jms.MessageConsumer; //導入依賴的package包/類
public MessageConsumer createTopicConsumer(String selector) throws JMSException {
	if (isQueue) {
		throw new IllegalArgumentException("Only for topic, not queue");
	}
	String consumerId = "consumer-" + UUID.randomUUID();
	topicConnection = startConnection(consumerId);
	Session session = topicConnection.createSession(isTransacted, Session.AUTO_ACKNOWLEDGE);
	Topic topic = session.createTopic(destinationName);
	if (isDurable) {
		if (selector != null) {
			return session.createDurableSubscriber(topic, consumerId, selector, true);
		} else {
			return session.createDurableSubscriber(topic, consumerId);
		}
	} else {
		if (selector != null) {
			return session.createConsumer(topic, selector);
		} else {
			return session.createConsumer(topic);
		}
	}
}
 
開發者ID:xtf-cz,項目名稱:xtf,代碼行數:23,代碼來源:JmsClient.java

示例4: consumeMessage

import javax.jms.MessageConsumer; //導入依賴的package包/類
/** Deletes the input Message from the given Queue, by creating a temporary consumer for that Message
 * @param session the JMS Session.
 * @param message the JMS Message.
 * @param queueName the Queue to consume from.
 * @throws FrameworkException in case any internal error occurs.
 * @throws ApplicationExceptions Indicates application error(s).
 */
static void consumeMessage(Session session, Message message, String queueName) throws FrameworkException, ApplicationExceptions {
    try {
        // Creates a consumer on the session for the given queueName, and specifying a selector having HEADER_JMS_MESSAGE_ID as the given messageId
        String selector = new StringBuilder(HEADER_JMS_MESSAGE_ID)
        .append("='")
        .append(message.getJMSMessageID())
        .append('\'')
        .toString();
        MessageConsumer consumer = session.createConsumer(JmsClientHelper.obtainQueue(queueName), selector);
        
        // Consume the message. Wait for 10 seconds at most
        Message m = consumer.receive(10000);
        if (m == null)
            throw new ApplicationExceptions(new JaffaMessagingApplicationException(JaffaMessagingApplicationException.MESSAGE_NOT_FOUND));
        consumer.close();
    } catch (JMSException e) {
        log.error("Error in consuming a JMS Message", e);
        throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.DELETE_ERROR, null, e);
    }
}
 
開發者ID:jaffa-projects,項目名稱:jaffa-framework,代碼行數:28,代碼來源:JmsBrowser.java

示例5: testReceive

import javax.jms.MessageConsumer; //導入依賴的package包/類
@Test
public void testReceive() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue, "Color = Red");

    assertNull(consumer.receive());

    consumer.close();

    try {
        consumer.receive();
        fail("Should not be able to interact with closed consumer");
    } catch (IllegalStateException ise) {}
}
 
開發者ID:messaginghub,項目名稱:pooled-jms,代碼行數:17,代碼來源:JmsPoolMessageConusmerTest.java

示例6: testReceiveTimed

import javax.jms.MessageConsumer; //導入依賴的package包/類
@Test
public void testReceiveTimed() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue, "Color = Red");

    assertNull(consumer.receive(1));

    consumer.close();

    try {
        consumer.receive(1);
        fail("Should not be able to interact with closed consumer");
    } catch (IllegalStateException ise) {}
}
 
開發者ID:messaginghub,項目名稱:pooled-jms,代碼行數:17,代碼來源:JmsPoolMessageConusmerTest.java

示例7: sendWithReplyToTemp

import javax.jms.MessageConsumer; //導入依賴的package包/類
private void sendWithReplyToTemp(ConnectionFactory cf, String serviceQueue) throws JMSException, InterruptedException {
    Connection connection = cf.createConnection();
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue tempQueue = session.createTemporaryQueue();
    TextMessage msg = session.createTextMessage("Request");
    msg.setJMSReplyTo(tempQueue);
    MessageProducer producer = session.createProducer(session.createQueue(serviceQueue));
    producer.send(msg);

    MessageConsumer consumer = session.createConsumer(tempQueue);
    Message replyMsg = consumer.receive();
    assertNotNull(replyMsg);

    LOG.debug("Reply message: {}", replyMsg);

    consumer.close();

    producer.close();
    session.close();
    connection.close();
}
 
開發者ID:messaginghub,項目名稱:pooled-jms,代碼行數:23,代碼來源:PooledConnectionTempQueueTest.java

示例8: validateFactoryCreationWithActiveMQLibraries

import javax.jms.MessageConsumer; //導入依賴的package包/類
/**
 * This test simply validates that {@link ConnectionFactory} can be setup by
 * pointing to the location of the client libraries at runtime. It uses
 * ActiveMQ which is not present at the POM but instead pulled from Maven
 * repo using {@link TestUtils#setupActiveMqLibForTesting(boolean)}, which
 * implies that for this test to run the computer must be connected to the
 * Internet. If computer is not connected to the Internet, this test will
 * quietly fail logging a message.
 */
@Test
public void validateFactoryCreationWithActiveMQLibraries() throws Exception {
    try {
        String libPath = TestUtils.setupActiveMqLibForTesting(true);

        TestRunner runner = TestRunners.newTestRunner(mock(Processor.class));
        JMSConnectionFactoryProvider cfProvider = new JMSConnectionFactoryProvider();
        runner.addControllerService("cfProvider", cfProvider);
        runner.setProperty(cfProvider, JMSConnectionFactoryProvider.BROKER_URI,
                "vm://localhost?broker.persistent=false");
        runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CLIENT_LIB_DIR_PATH, libPath);
        runner.setProperty(cfProvider, JMSConnectionFactoryProvider.CONNECTION_FACTORY_IMPL,
                "org.apache.activemq.ActiveMQConnectionFactory");
        runner.enableControllerService(cfProvider);
        runner.assertValid(cfProvider);

        Connection connection = cfProvider.getConnectionFactory().createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination queue = session.createQueue("myqueue");
        MessageProducer producer = session.createProducer(queue);
        MessageConsumer consumer = session.createConsumer(queue);

        TextMessage message = session.createTextMessage("Hello");
        producer.send(message);
        assertEquals("Hello", ((TextMessage) consumer.receive()).getText());
        connection.stop();
        connection.close();
    } catch (Exception e) {
        logger.error("'validateFactoryCreationWithActiveMQLibraries' failed due to ", e);
    }
}
 
開發者ID:SolaceLabs,項目名稱:solace-integration-guides,代碼行數:43,代碼來源:JMSConnectionFactoryProviderTest.java

示例9: JmsMessageSource

import javax.jms.MessageConsumer; //導入依賴的package包/類
/**
 * JMS Consumer and Converter to convert retrieve and convert Message.
 * @param consumer The consumer
 * @param converter The converter
 */
public JmsMessageSource(MessageConsumer consumer, JmsMessageConverter converter) {
  try {
    this.converter = converter;
    consumer.setMessageListener(this);
  } catch (JMSException ex) {
    throw new RuntimeException(ex);
  }
}
 
開發者ID:sventorben,項目名稱:axon-jms,代碼行數:14,代碼來源:JmsMessageSource.java

示例10: createConsumer

import javax.jms.MessageConsumer; //導入依賴的package包/類
@Override
public MessageConsumer createConsumer(Destination destination, String messageSelector) throws JMSException {
    if (destination instanceof Topic) {
        throw new IllegalStateException("Operation not supported by a QueueSession");
    }
    return super.createConsumer(destination, messageSelector);
}
 
開發者ID:messaginghub,項目名稱:pooled-jms,代碼行數:8,代碼來源:MockJMSQueueSession.java

示例11: createConsumer

import javax.jms.MessageConsumer; //導入依賴的package包/類
public MessageConsumer createConsumer(Destination destination,
        ProxySession session) throws JMSException {
    String destinationName = destination.toString();
    ProxyMessageConsumer messageConsumer = new ProxyMessageConsumer(session);
    messageConsumer.setDestination(destination);

    if (destination instanceof Topic) {
        this.messageHandler.registerToTopic(destinationName,
                messageConsumer.getId());
    }

    return messageConsumer;
}
 
開發者ID:zhaojunfei,項目名稱:lemon,代碼行數:14,代碼來源:ProxyConnectionFactory.java

示例12: createConsumer

import javax.jms.MessageConsumer; //導入依賴的package包/類
/**
 * @see javax.jms.Session#createConsumer(javax.jms.Destination, java.lang.String)
 */
@Override
public MessageConsumer createConsumer(Destination destination, String messageSelector, boolean noLocal) throws JMSException {
    if (destination instanceof Queue) {
        throw new IllegalStateException("Operation not supported by a TopicSession");
    }
    return super.createConsumer(destination, messageSelector, noLocal);
}
 
開發者ID:messaginghub,項目名稱:pooled-jms,代碼行數:11,代碼來源:MockJMSTopicSession.java

示例13: testToString

import javax.jms.MessageConsumer; //導入依賴的package包/類
@Test
public void testToString() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue);

    assertNotNull(consumer.toString());
}
 
開發者ID:messaginghub,項目名稱:pooled-jms,代碼行數:10,代碼來源:JmsPoolMessageConusmerTest.java

示例14: testCloseMoreThanOnce

import javax.jms.MessageConsumer; //導入依賴的package包/類
@Test
public void testCloseMoreThanOnce() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue);

    consumer.close();
    consumer.close();
}
 
開發者ID:messaginghub,項目名稱:pooled-jms,代碼行數:11,代碼來源:JmsPoolMessageConusmerTest.java

示例15: setup

import javax.jms.MessageConsumer; //導入依賴的package包/類
@Before
public void setup() throws Exception {
  beforeSetup();
  connectionFactory = mock(ConnectionFactory.class);
  connection = mock(Connection.class);
  session = mock(Session.class);
  queue = mock(Queue.class);
  topic = mock(Topic.class);
  messageConsumer = mock(MessageConsumer.class);
  message = mock(TextMessage.class);
  when(message.getPropertyNames()).thenReturn(new Enumeration<Object>() {
    @Override
    public boolean hasMoreElements() {
      return false;
    }
    @Override
    public Object nextElement() {
      throw new UnsupportedOperationException();
    }
  });
  when(message.getText()).thenReturn(TEXT);
  when(connectionFactory.createConnection(USERNAME, PASSWORD)).thenReturn(connection);
  when(connection.createSession(true, Session.SESSION_TRANSACTED)).thenReturn(session);
  when(session.createQueue(destinationName)).thenReturn(queue);
  when(session.createConsumer(any(Destination.class), anyString())).thenReturn(messageConsumer);
  when(messageConsumer.receiveNoWait()).thenReturn(message);
  when(messageConsumer.receive(anyLong())).thenReturn(message);
  destinationName = DESTINATION_NAME;
  destinationType = JMSDestinationType.QUEUE;
  destinationLocator = JMSDestinationLocator.CDI;
  messageSelector = SELECTOR;
  batchSize = 10;
  pollTimeout = 500L;
  context = new Context();
  converter = new DefaultJMSMessageConverter.Builder().build(context);
  event = converter.convert(message).iterator().next();
  userName = Optional.of(USERNAME);
  password = Optional.of(PASSWORD);
  afterSetup();
}
 
開發者ID:moueimei,項目名稱:flume-release-1.7.0,代碼行數:41,代碼來源:JMSMessageConsumerTestBase.java


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