List of usage examples for javax.jms QueueReceiver receive
Message receive(long timeout) throws JMSException;
From source file:io.datalayer.activemq.consumer.SimpleQueueReceiver.java
/** * Main method.//from ww w .j a v a 2s. c o m * * @param args the queue used by the example */ public static void main(String... args) { String queueName = null; Context jndiContext = null; QueueConnectionFactory queueConnectionFactory = null; QueueConnection queueConnection = null; QueueSession queueSession = null; Queue queue = null; QueueReceiver queueReceiver = null; TextMessage message = null; /* * Read queue name from command line and display it. */ if (args.length != 1) { LOG.info("Usage: java " + "SimpleQueueReceiver <queue-name>"); System.exit(1); } queueName = args[0]; LOG.info("Queue name is " + queueName); /* * Create a JNDI API InitialContext object if none exists yet. */ try { jndiContext = new InitialContext(); } catch (NamingException e) { LOG.info("Could not create JNDI API " + "context: " + e.toString()); System.exit(1); } /* * Look up connection factory and queue. If either does not exist, exit. */ try { queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory"); queue = (Queue) jndiContext.lookup(queueName); } catch (NamingException e) { LOG.info("JNDI API lookup failed: " + e.toString()); System.exit(1); } /* * Create connection. Create session from connection; false means * session is not transacted. Create receiver, then start message * delivery. Receive all text messages from queue until a non-text * message is received indicating end of message stream. Close * connection. */ try { queueConnection = queueConnectionFactory.createQueueConnection(); queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queueReceiver = queueSession.createReceiver(queue); queueConnection.start(); while (true) { Message m = queueReceiver.receive(1); if (m != null) { if (m instanceof TextMessage) { message = (TextMessage) m; LOG.info("Reading message: " + message.getText()); } else { break; } } } } catch (JMSException e) { LOG.info("Exception occurred: " + e.toString()); } finally { if (queueConnection != null) { try { queueConnection.close(); } catch (JMSException e) { } } } }
From source file:nl.nn.adapterframework.extensions.ifsa.jms.IfsaRequesterSender.java
/** * Retrieves a message with the specified correlationId from queue or other channel, but does no processing on it. *//* w ww . j a va2 s .c o m*/ private Message getRawReplyMessage(QueueSession session, IFSAQueue queue, TextMessage sentMessage) throws SenderException, TimeOutException { String selector = null; Message msg = null; QueueReceiver replyReceiver = null; try { replyReceiver = getReplyReceiver(session, sentMessage); selector = replyReceiver.getMessageSelector(); long timeout = getExpiry(queue); log.debug(getLogPrefix() + "start waiting at most [" + timeout + "] ms for reply on message using selector [" + selector + "]"); msg = replyReceiver.receive(timeout); if (msg == null) { log.info(getLogPrefix() + "received null reply"); } else { log.info(getLogPrefix() + "received reply"); } } catch (Exception e) { throw new SenderException(getLogPrefix() + "got exception retrieving reply", e); } finally { try { closeReplyReceiver(replyReceiver); } catch (IfsaException e) { log.error(getLogPrefix() + "error closing replyreceiver", e); } } if (msg == null) { throw new TimeOutException( getLogPrefix() + " timed out waiting for reply using selector [" + selector + "]"); } if (msg instanceof IFSATimeOutMessage) { throw new TimeOutException(getLogPrefix() + "received IFSATimeOutMessage waiting for reply using selector [" + selector + "]"); } return msg; // try { // TextMessage result = (TextMessage)msg; // return result; // } catch (Exception e) { // throw new SenderException(getLogPrefix()+"reply received for message using selector ["+selector+"] cannot be cast to TextMessage ["+msg.getClass().getName()+"]",e); // } }
From source file:nl.nn.adapterframework.extensions.ifsa.jms.PullingIfsaProviderListener.java
/** * Retrieves messages to be processed by the server, implementing an IFSA-service, but does no processing on it. *///w w w. j a v a2s. co m public Object getRawMessage(Map threadContext) throws ListenerException { Object result = null; QueueSession session = null; QueueReceiver receiver = null; threadContext.remove(THREAD_CONTEXT_ORIGINAL_RAW_MESSAGE_KEY); try { session = getSession(threadContext); try { receiver = getReceiver(threadContext, session); result = receiver.receive(getTimeOut()); while (result == null && canGoOn() && !JtaUtil.inTransaction()) { result = receiver.receive(getTimeOut()); } } catch (Exception e) { throw new ListenerException(getLogPrefix(), e); } finally { releaseReceiver(receiver); } } finally { if (sessionNeedsToBeSavedForAfterProcessMessage(result)) { threadContext.put(THREAD_CONTEXT_SESSION_KEY, session); } else { releaseSession(session); } } if (result instanceof IFSAPoisonMessage) { IFSAHeader header = ((IFSAPoisonMessage) result).getIFSAHeader(); String source; try { source = header.getIFSA_Source(); } catch (Exception e) { source = "unknown due to exeption:" + e.getMessage(); } String msg = getLogPrefix() + "received IFSAPoisonMessage " + "source [" + source + "]" + "content [" + ToStringBuilder.reflectionToString((IFSAPoisonMessage) result) + "]"; log.warn(msg); } try { if ((result instanceof IFSATextMessage || result instanceof IFSAPoisonMessage) && JtaUtil.inTransaction()) { threadContext.put(THREAD_CONTEXT_ORIGINAL_RAW_MESSAGE_KEY, result); result = new MessageWrapper(result, this); } } catch (Exception e) { throw new ListenerException("cannot wrap non serialzable message in wrapper", e); } return result; }
From source file:org.grouter.common.jms.QueueSenderDestination.java
/** * <b>See documentation in {@link org.grouter.common.jms.AbstractSenderDestination#waitAndGetReplyFromTemporaryDestination(long)}.</b><br> * <br>/*from w ww . j av a2 s .co m*/ */ public Message waitAndGetReplyFromTemporaryDestination(long waitForMs) { QueueReceiver receiver = null; try { if (!useTemporaryReplyDestination) { throw new IllegalStateException("You have used this destination in a wrong way. Have you " + "used correct constructor for temporary destinations? Use constructor" + "where you indicate that you should be using a temporary Q. "); } receiver = queueSession.createReceiver(getTemporaryQueue()); return receiver.receive(waitForMs); } catch (JMSException ex) { logger.warn("Waiting for reply on temp queue failed", ex); return null; } finally { if (receiver != null) { try { receiver.close(); } catch (Exception ex1) { //ignore } } } }
From source file:org.smartfrog.avalanche.shared.jms.MessageListener.java
public MonitoringEvent receive() throws Exception { MonitoringEvent event = null;//from www . j a v a 2 s . c om // TODO : no need to open a new session every time .. fix it QueueSession qs = null; QueueReceiver qr = null; try { qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); qr = qs.createReceiver(queue); // TODO : Fix message timeout log.info("MonitoringEvent.receive() : Checking for new message on Queue"); MapMessage mm = (MapMessage) qr.receive(jmsTimeout); if (mm != null) { log.info("Message received"); event = new MonitoringEventDefaultImpl(); event.setHost(mm.getString(MonitoringEvent.HOST)); event.setInstanceName(mm.getString(MonitoringEvent.INSTANCE_NAME)); event.setModuleId(mm.getString(MonitoringEvent.MODULEID)); event.setModuleState(mm.getString(MonitoringEvent.MODULE_STATE)); event.setMsg(mm.getString(MonitoringEvent.MODULE_STATE)); event.setMessageType(mm.getInt(MonitoringEvent.MESSAGE_TYPE)); log.info("MessageListener.receive() - " + event); } else { log.info("No message found in queue"); } return event; } finally { qr.close(); qs.close(); } }