List of usage examples for javax.jms MessageConsumer setMessageListener
void setMessageListener(MessageListener listener) throws JMSException;
From source file:org.dawnsci.commandserver.core.producer.AliveConsumer.java
protected void createTerminateListener() throws Exception { ConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri); this.terminateConnection = connectionFactory.createConnection(); terminateConnection.start();//from w w w. j av a 2 s. c om Session session = terminateConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); final Topic topic = session.createTopic(Constants.TERMINATE_CONSUMER_TOPIC); final MessageConsumer consumer = session.createConsumer(topic); final ObjectMapper mapper = new ObjectMapper(); MessageListener listener = new MessageListener() { public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage t = (TextMessage) message; final ConsumerBean bean = mapper.readValue(t.getText(), ConsumerBean.class); if (bean.getStatus().isFinal()) { // Something else already happened terminateConnection.close(); return; } if (consumerId.equals(bean.getConsumerId())) { if (bean.getStatus() == ConsumerStatus.REQUEST_TERMINATE) { System.out.println(getName() + " has been requested to terminate and will exit."); cbean.setStatus(ConsumerStatus.REQUEST_TERMINATE); Thread.currentThread().sleep(2500); System.exit(0); } } } } catch (Exception e) { e.printStackTrace(); } } }; consumer.setMessageListener(listener); }
From source file:tools.ConsumerTool.java
@Override public void run() { Connection connection = null; Session session = null;/*from ww w . j av a 2s . c o m*/ try { connection = connectionFactory.createConnection(); if (clientId != null) { connection.setClientID(clientId); } connection.start(); session = connection.createSession(transacted, acknowledgeMode); Destination destination = null; if (jndiLookupDestinations) { destination = (Destination) context.lookup(destinationName); } else { if (useQueueDestinations) { if (useTemporaryDestinations) { destination = session.createTemporaryQueue(); } else { destination = session.createQueue(destinationName); } } else { if (useTemporaryDestinations) { destination = session.createTemporaryTopic(); } else { destination = session.createTopic(destinationName); } } } if (useQueueBrowser) { runQueueBrowser(session, (Queue) destination); } else { MessageConsumer consumer = null; if (useQueueDestinations) { //Queues if (selector != null) { consumer = session.createConsumer(destination, selector); } else { consumer = session.createConsumer(destination); } } else { //Queues if (durable) { //Durable Subscribers if (selector != null) { consumer = session.createDurableSubscriber((Topic) destination, subscriptionName, selector, false); } else { consumer = session.createDurableSubscriber((Topic) destination, subscriptionName); } } else { //Non-Durable Subscribers if (selector != null) { consumer = session.createConsumer(destination, selector); } else { consumer = session.createConsumer(destination); } } } if (useAsyncListener) { final Session consumerSession = session; final AtomicInteger perConsumerReceivedMessages = new AtomicInteger(0); consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { perConsumerReceivedMessages.incrementAndGet(); handleMessage(consumerSession, message, perConsumerReceivedMessages.get()); } }); while (perConsumerReceivedMessages.get() < numMessages) { Thread.sleep(100); } } else { int perConsumerReceivedMessages = 0; while (perConsumerReceivedMessages < numMessages) { Message message = null; if (receiveTimeoutMS > -1) { message = consumer.receive(receiveTimeoutMS); } else { message = consumer.receive(); } if (message != null) { perConsumerReceivedMessages++; handleMessage(session, message, perConsumerReceivedMessages); } } } consumer.close(); } } catch (Exception ex) { LOGGER.error("ConsumerTool hit exception: " + ex.getMessage(), ex); } finally { if (session != null) { try { session.close(); } catch (JMSException e) { LOGGER.error("JMSException closing session", e); } } if (connection != null) { try { connection.close(); } catch (JMSException e) { LOGGER.error("JMSException closing connection", e); } } } }
From source file:org.dawnsci.commandserver.ui.view.StatusQueueView.java
/** * Listens to a topic/*from w ww . j av a 2 s . c om*/ */ private void createTopicListener(final URI uri) throws Exception { // Use job because connection might timeout. final Job topicJob = new Job("Create topic listener") { @Override protected IStatus run(IProgressMonitor monitor) { try { ConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri); StatusQueueView.this.topicConnection = connectionFactory.createConnection(); topicConnection.start(); Session session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(getTopicName()); MessageConsumer consumer = session.createConsumer(topic); final Class clazz = getBeanClass(); final ObjectMapper mapper = new ObjectMapper(); MessageListener listener = new MessageListener() { public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage t = (TextMessage) message; final StatusBean bean = mapper.readValue(t.getText(), clazz); mergeBean(bean); } } catch (Exception e) { logger.error("Updating changed bean from topic", e); } } }; consumer.setMessageListener(listener); // Create a listener for administrator broadcast messages. topic = session.createTopic(Constants.ADMIN_MESSAGE_TOPIC); consumer = session.createConsumer(topic); listener = new MessageListener() { public void onMessage(Message message) { try { if (message instanceof TextMessage) { // AdministratorMessage shows a message to the user. final Class msgClass = AdministratorMessage.class; TextMessage t = (TextMessage) message; final AdministratorMessage bean = mapper.readValue(t.getText(), msgClass); getSite().getShell().getDisplay().syncExec(new Runnable() { public void run() { MessageDialog.openError(getViewSite().getShell(), bean.getTitle(), bean.getMessage()); viewer.refresh(); } }); } } catch (Exception e) { // Not a big deal if they do not get admin messages. } } }; consumer.setMessageListener(listener); return Status.OK_STATUS; } catch (Exception ne) { logger.error("Cannot listen to topic changes because command server is not there", ne); return Status.CANCEL_STATUS; } } }; topicJob.setPriority(Job.INTERACTIVE); topicJob.setSystem(true); topicJob.setUser(false); topicJob.schedule(); }
From source file:org.dawnsci.commandserver.core.process.ProgressableProcess.java
/** * Starts a connection which listens to the topic and if * a cancel is found published, tries to terminate the subprocess. * /*w ww. j a v a 2 s. co m*/ * @param p */ protected void createTerminateListener() throws Exception { ConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri); ProgressableProcess.this.topicConnection = connectionFactory.createConnection(); topicConnection.start(); Session session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); final Topic topic = session.createTopic(statusTName); final MessageConsumer consumer = session.createConsumer(topic); final Class clazz = bean.getClass(); final ObjectMapper mapper = new ObjectMapper(); MessageListener listener = new MessageListener() { public void onMessage(Message message) { try { if (message instanceof TextMessage) { TextMessage t = (TextMessage) message; final StatusBean tbean = mapper.readValue(t.getText(), clazz); if (bean.getStatus().isFinal()) { // Something else already happened topicConnection.close(); return; } if (bean.getUniqueId().equals(tbean.getUniqueId())) { if (tbean.getStatus() == Status.REQUEST_TERMINATE) { bean.merge(tbean); out.println("Terminating job '" + tbean.getName() + "'"); terminate(); topicConnection.close(); bean.setStatus(Status.TERMINATED); bean.setMessage("Foricibly terminated before finishing."); broadcast(bean); return; } } } } catch (Exception e) { e.printStackTrace(); } } }; consumer.setMessageListener(listener); }
From source file:com.tremolosecurity.provisioning.core.ProvisioningEngineImpl.java
public void lazyInit(ConfigManager cfgMgr) throws ProvisioningException { try {// w w w. j ava2 s. c o m javax.jms.Connection connection = prov.getQueueConnection(); //((ActiveMQConnection)connection ).getRedeliveryPolicy().setMaximumRedeliveries(3); //connection.start(); this.session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); smtpQueue = "TremoloUnisonSMTPQueue"; if (cfgMgr.getCfg().getProvisioning() != null && cfgMgr.getCfg().getProvisioning().getQueueConfig() != null) { smtpQueue = cfgMgr.getCfg().getProvisioning().getQueueConfig().getSmtpQueueName(); } javax.jms.Queue queue = session.createQueue(smtpQueue); this.mp = session.createProducer(queue); MessageConsumer mc = session.createConsumer(queue); mc.setMessageListener(this); cfgMgr.addThread(new JMSMessageCloser(session, mc)); } catch (JMSException e) { throw new ProvisioningException("Could not initialize JMS", e); } }
From source file:com.tremolosecurity.provisioning.core.ProvisioningEngineImpl.java
@Override public void initListeners() throws ProvisioningException { if (this.cfgMgr.getCfg().getProvisioning() == null || this.cfgMgr.getCfg().getProvisioning().getListeners() == null) { logger.warn("No listeners defined"); return;// w w w . ja v a 2 s. com } try { javax.jms.Connection con = this.getQueueConnection(); for (MessageListenerType mlt : this.cfgMgr.getCfg().getProvisioning().getListeners().getListener()) { UnisonMessageListener uml = (UnisonMessageListener) Class.forName(mlt.getClassName()).newInstance(); HashMap<String, Attribute> attrs = new HashMap<String, Attribute>(); for (ParamType pt : mlt.getParams()) { Attribute attr = attrs.get(pt.getName()); if (attr == null) { attr = new Attribute(pt.getName()); attrs.put(pt.getName(), attr); } attr.getValues().add(pt.getValue()); } uml.init(this.cfgMgr, attrs); javax.jms.Session listenerSession = con.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); javax.jms.Queue taskQueue = listenerSession.createQueue(mlt.getQueueName()); MessageConsumer mc = listenerSession.createConsumer(taskQueue); mc.setMessageListener(uml); cfgMgr.addThread(new JMSMessageCloser(con, listenerSession, mc)); } } catch (Exception e) { logger.warn("Could not initialize listeners", e); } }
From source file:org.seedstack.jms.internal.JmsPlugin.java
private MessageConsumer createMessageConsumer(MessageListenerDefinition messageListenerDefinition) throws JMSException { LOGGER.debug("Creating JMS consumer for listener {}", messageListenerDefinition.getName()); MessageConsumer consumer; Session session = messageListenerDefinition.getSession(); if (StringUtils.isNotBlank(messageListenerDefinition.getSelector())) { consumer = session.createConsumer(messageListenerDefinition.getDestination(), messageListenerDefinition.getSelector()); } else {/*from w w w .ja v a 2 s.com*/ consumer = session.createConsumer(messageListenerDefinition.getDestination()); } MessagePoller messagePoller; if (messageListenerDefinition.getPoller() != null) { try { LOGGER.debug("Creating poller for JMS listener {}", messageListenerDefinition.getName()); Connection connection = connections.get(messageListenerDefinition.getConnectionName()); messagePoller = messageListenerDefinition.getPoller().newInstance(); messagePoller.setSession(session); messagePoller.setMessageConsumer(consumer); messagePoller.setMessageListener(new MessageListenerAdapter(messageListenerDefinition.getName())); if (connection instanceof ManagedConnection) { messagePoller.setExceptionListener((ExceptionListener) connection); } else { messagePoller.setExceptionListener(connection.getExceptionListener()); } } catch (Exception e) { throw SeedException.wrap(e, JmsErrorCodes.UNABLE_TO_CREATE_POLLER).put("pollerClass", messageListenerDefinition.getPoller()); } pollers.put(messageListenerDefinition.getName(), messagePoller); } else { consumer.setMessageListener(new MessageListenerAdapter(messageListenerDefinition.getName())); } return consumer; }
From source file:com.tremolosecurity.provisioning.core.ProvisioningEngineImpl.java
@Override public void initMessageConsumers() throws ProvisioningException { try {/*from ww w . j ava 2 s . c o m*/ if (cfgMgr.getCfg().getProvisioning() == null) { return; } String taskQueueName = "TremoloUnisonTaskQueue"; if (this.cfgMgr.getCfg().getProvisioning() != null && this.cfgMgr.getCfg().getProvisioning().getQueueConfig() != null) { taskQueueName = this.cfgMgr.getCfg().getProvisioning().getQueueConfig().getTaskQueueName(); } if (this.isInternalQueue()) { javax.jms.Connection con = this.getQueueConnection(); TaskConsumer taskConsumer = new TaskConsumer(this, this.cfgMgr); this.taskSession = con.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); this.taskQueue = taskSession.createQueue(taskQueueName); this.taskMP = taskSession.createProducer(taskQueue); MessageConsumer mc = taskSession.createConsumer(taskQueue); mc.setMessageListener(taskConsumer); cfgMgr.addThread(new JMSMessageCloser(taskSession, mc)); } else { if (this.cfgMgr.getCfg().getProvisioning().getQueueConfig().isMultiTaskQueues()) { for (int j = 1; j <= this.cfgMgr.getCfg().getProvisioning().getQueueConfig() .getNumQueues(); j++) { String name = this.cfgMgr.getCfg().getProvisioning().getQueueConfig().getTaskQueueName() .replace("{x}", Integer.toString(j)); for (int i = 0; i < this.cfgMgr.getCfg().getProvisioning().getQueueConfig() .getMaxConsumers(); i++) { javax.jms.Connection con = this.getQueueConnection(); TaskConsumer taskConsumer = new TaskConsumer(this, this.cfgMgr); javax.jms.Session taskSession = con.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); javax.jms.Queue taskQueue = taskSession.createQueue(name); MessageConsumer mc = taskSession.createConsumer(taskQueue); mc.setMessageListener(taskConsumer); cfgMgr.addThread(new JMSMessageCloser(con, taskSession, mc)); } } } else { for (int i = 0; i < this.cfgMgr.getCfg().getProvisioning().getQueueConfig() .getMaxConsumers(); i++) { javax.jms.Connection con = this.getQueueConnection(); TaskConsumer taskConsumer = new TaskConsumer(this, this.cfgMgr); javax.jms.Session taskSession = con.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); javax.jms.Queue taskQueue = taskSession.createQueue(taskQueueName); MessageConsumer mc = taskSession.createConsumer(taskQueue); mc.setMessageListener(taskConsumer); cfgMgr.addThread(new JMSMessageCloser(con, taskSession, mc)); } } } } catch (JMSException e) { throw new ProvisioningException("Could not initialize task message system", e); } }