当前位置: 首页>>代码示例>>Java>>正文


Java MessageConsumer.close方法代码示例

本文整理汇总了Java中javax.jms.MessageConsumer.close方法的典型用法代码示例。如果您正苦于以下问题:Java MessageConsumer.close方法的具体用法?Java MessageConsumer.close怎么用?Java MessageConsumer.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.jms.MessageConsumer的用法示例。


在下文中一共展示了MessageConsumer.close方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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

示例3: testReceiveNoWait

import javax.jms.MessageConsumer; //导入方法依赖的package包/类
@Test
public void testReceiveNoWait() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue, "Color = Red");

    assertNull(consumer.receiveNoWait());

    consumer.close();

    try {
        consumer.receiveNoWait();
        fail("Should not be able to interact with closed consumer");
    } catch (IllegalStateException ise) {}
}
 
开发者ID:messaginghub,项目名称:pooled-jms,代码行数:17,代码来源:JmsPoolMessageConusmerTest.java

示例4: 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

示例5: testGetMessageSelector

import javax.jms.MessageConsumer; //导入方法依赖的package包/类
@Test
public void testGetMessageSelector() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue, "Color = Red");

    assertNotNull(consumer.getMessageSelector());
    assertEquals("Color = Red", consumer.getMessageSelector());

    consumer.close();

    try {
        consumer.getMessageSelector();
        fail("Should not be able to interact with closed consumer");
    } catch (IllegalStateException ise) {}
}
 
开发者ID:messaginghub,项目名称:pooled-jms,代码行数:18,代码来源:JmsPoolMessageConusmerTest.java

示例6: 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

示例7: 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

示例8: 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

示例9: main

import javax.jms.MessageConsumer; //导入方法依赖的package包/类
public static void main(String[] args) {

        try {

            // Create a ConnectionFactory
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://Toshiba:61616");

            // 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("HELLOWORLD.TESTQ");

            // Create a MessageConsumer from the Session to the Topic or Queue
            MessageConsumer consumer = session.createConsumer(destination);

            // Wait for a message
            Message message = consumer.receive(1000);

            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                String text = textMessage.getText();
                System.out.println("Received: " + text);
            } else {
                System.out.println("Received: " + message);
            }

            consumer.close();
            session.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
	}
 
开发者ID:Illusionist80,项目名称:SpringTutorial,代码行数:41,代码来源:HelloWorldConsumer.java

示例10: run

import javax.jms.MessageConsumer; //导入方法依赖的package包/类
public void run() {
    try {

        // Create a ConnectionFactory
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://Toshiba:61616");

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

        connection.setExceptionListener(this);

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

        // Create the destination (Topic or Queue)
        Destination destination = session.createQueue("HELLOWORLD.TESTQ");

        // Create a MessageConsumer from the Session to the Topic or Queue
        MessageConsumer consumer = session.createConsumer(destination);

        // Wait for a message
        Message message = consumer.receive(1000);

        if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            String text = textMessage.getText();
            System.out.println("Received: " + text);
        } else {
            System.out.println("Received: " + message);
        }

        consumer.close();
        session.close();
        connection.close();
    } catch (Exception e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }
}
 
开发者ID:Illusionist80,项目名称:SpringTutorial,代码行数:41,代码来源:HelloWorldConsumerThread.java

示例11: requeueFailedMessages

import javax.jms.MessageConsumer; //导入方法依赖的package包/类
@Override
public void requeueFailedMessages() {
    try {
        ActiveMQConnection connection = ActiveMqUtils.openConnection(user, password, url);
        Session session = ActiveMqUtils.startSession(connection);

        int count = getQueueSize(session, queueError);

        if (count < 1) {
            return;
        }

        log.info("Requeuing {} failed messages...", count);

        Queue queueErr = session.createQueue(queueError);
        MessageConsumer consumer = session.createConsumer(queueErr);

        Queue queueRetry = session.createQueue(queueInput);
        MessageProducer producer = session.createProducer(queueRetry);

        for (int consumed = 0; consumed < count; consumed++) {
            TextMessage message = (TextMessage) consumer.receive(REQUEUE_TIMEOUT);

            if (message == null) {
                continue;
            }

            String text = message.getText();
            String requestId = message.getJMSCorrelationID();

            log.info("Requeuing message '{}'", text);

            try {
                TextMessage newMessage = session.createTextMessage(text);
                newMessage.setJMSCorrelationID(requestId);

                producer.send(newMessage);
            } catch (Exception e) {
                log.error("Failed to requeue message", e);
            }

            message.acknowledge();
            session.commit();
        }

        producer.close();
        consumer.close();
    } catch (JMSException ex) {
        throw new MessageQueueException("Failed to requeue failed messages", ex);
    }
}
 
开发者ID:code-obos,项目名称:servicebuilder,代码行数:52,代码来源:ActiveMqListener.java

示例12: startConsumer

import javax.jms.MessageConsumer; //导入方法依赖的package包/类
private void startConsumer(){
  ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl);
  pooledConnectionFactory = new PooledConnectionFactory(factory);
  pooledConnectionFactory.setReconnectOnException(true);
  pooledConnectionFactory.setMaximumActiveSessionPerConnection(10); // 10
  pooledConnectionFactory.setMaxConnections(10); // 10
  pooledConnectionFactory.setIdleTimeout(300000); // 管理连接池中的连接,5分钟超时   300000
  pooledConnectionFactory.setUseAnonymousProducers(false);
  pooledConnectionFactory.start();
  Thread t = new Thread(){
    @Override
    public void run() {
      // consumer
      Connection conn ;
      Session sess;
      MessageConsumer consumer;
      try {
        while(!shutdown){
          conn = pooledConnectionFactory.createConnection();
          conn.start();
          sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
          Destination dest = sess.createQueue("_eds_"+queueName);
          consumer = sess.createConsumer(dest);
          Message msg = consumer.receive();

          if(msg != null){
            System.out.println("received => "+msg);
            msgReceived.incrementAndGet();
          }
          consumer.close();
          sess.close();
          conn.close();
          System.out.println("receiving => "+msg);
        }
      } catch (JMSException e) {
        e.printStackTrace();
      }

    }
  };
  t.start();
}
 
开发者ID:eXcellme,项目名称:eds,代码行数:43,代码来源:EdsTest.java

示例13: getBeans

import javax.jms.MessageConsumer; //导入方法依赖的package包/类
/**
 * Read the status beans from any queue.
 * Returns a list of optionally date-ordered beans in the queue.
 *
 * @param uri
 * @param queueName
 * @param clazz
 * @param monitor
 * @return
 * @throws Exception
 */
public List<T> getBeans(final URI uri, final String queueName, final Class<T> beanClass) throws Exception {

	QueueConnection qCon = null;
	try {
		QueueConnectionFactory connectionFactory = (QueueConnectionFactory)service.createConnectionFactory(uri);
		qCon  = connectionFactory.createQueueConnection(); // This times out when the server is not there.
		QueueSession    qSes  = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
		Queue queue   = qSes.createQueue(queueName);
		qCon.start();

		QueueBrowser qb = qSes.createBrowser(queue);
		@SuppressWarnings("rawtypes")
		Enumeration  e  = qb.getEnumeration();


		final Collection<T> list;
		if (comparator!=null) {
			list = new TreeSet<T>(comparator);
		} else {
			list = new ArrayList<T>(17);
		}

		while(e.hasMoreElements()) {
			Message m = (Message)e.nextElement();
			if (m==null) continue;
			if (m instanceof TextMessage) {
				TextMessage t = (TextMessage)m;
				String json   = t.getText();
				@SuppressWarnings("unchecked")
				final Class<T> statusBeanClass = (Class<T>) StatusBean.class;
				try {
					final T bean = (T)service.unmarshal(json, beanClass != null ? beanClass : statusBeanClass);
					list.add(bean);

				} catch (Exception unmarshallable) {
					System.out.println("Removing old message "+json);
					String jMSMessageID = m.getJMSMessageID();
					if (jMSMessageID!=null) {
						MessageConsumer consumer = qSes.createConsumer(queue, "JMSMessageID = '"+jMSMessageID+"'");
						Message ignored = consumer.receive(1000);
						consumer.close();
						System.out.println("Removed");
					}

				}
			}
		}
		return list instanceof List ? (List<T>)list : new ArrayList<T>(list);

	} finally {
		if (qCon!=null) qCon.close();
	}

}
 
开发者ID:eclipse,项目名称:scanning,代码行数:66,代码来源:QueueReader.java

示例14: receiveMessage

import javax.jms.MessageConsumer; //导入方法依赖的package包/类
public Message receiveMessage(long timeout, String selector) throws JMSException {
	Connection connection = null;
	Message result = null;
	try {
		connection = startConnection(); //try to be smarter here and start stable connection
		Session session = null;
		try {
			session = connection.createSession(isTransacted, Session.AUTO_ACKNOWLEDGE);
			Destination dest;
			if (isQueue) {
				dest = session.createQueue(destinationName);
			} else {
				dest = session.createTopic(destinationName);
			}
			MessageConsumer consumer;
			if (selector != null) {
				consumer = session.createConsumer(dest, selector);
			} else {
				consumer = session.createConsumer(dest);
			}
			try {
				result = consumer.receive(timeout);
			} finally {
				if (consumer != null) consumer.close();
			}
		} finally {
			if (session != null) session.close();
		}
	} finally {
		safeCloseConnection(connection);
	}
	return result;
}
 
开发者ID:xtf-cz,项目名称:xtf,代码行数:34,代码来源:JmsClient.java


注:本文中的javax.jms.MessageConsumer.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。