本文整理汇总了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();
}
示例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) {}
}
示例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) {}
}
示例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) {}
}
示例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) {}
}
示例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();
}
示例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);
}
}
示例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();
}
示例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();
}
}
示例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();
}
}
示例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);
}
}
示例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();
}
示例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();
}
}
示例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;
}