List of usage examples for javax.jms QueueConnection createQueueSession
QueueSession createQueueSession(boolean transacted, int acknowledgeMode) throws JMSException;
From source file:org.apache.servicemix.jms.JmsMarshalerTest.java
public void testMarshalTextMessage() throws Exception { JmsComponent jms = new JmsComponent(); jms.getConfiguration().setConnectionFactory(connectionFactory); JmsEndpoint ep = new JmsEndpoint(); ep.setService(ReceiverComponent.SERVICE); ep.setEndpoint("jms"); ep.setTargetService(ReceiverComponent.SERVICE); ep.setTargetEndpoint(ReceiverComponent.ENDPOINT); ep.setRole(MessageExchange.Role.CONSUMER); ep.setDestinationStyle(AbstractJmsProcessor.STYLE_QUEUE); ep.setDestination(queue);/*from www . j av a 2 s .c o m*/ ep.setDefaultMep(JbiConstants.IN_ONLY); ep.setMarshaler(new DefaultJmsMarshaler(ep)); jms.setEndpoints(new JmsEndpoint[] { ep }); container.activateComponent(jms, "servicemix-jms"); ReceiverComponent receiver = new ReceiverComponent(); container.activateComponent(receiver, "receiver"); QueueConnection qConn = connectionFactory.createQueueConnection(); QueueSession qSess = qConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); QueueSender qSender = qSess.createSender(queue); TextMessage message = qSess .createTextMessage("<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world</hello>"); qSender.send(message); receiver.getMessageList().assertMessagesReceived(1); List msgs = receiver.getMessageList().flushMessages(); NormalizedMessage msg = (NormalizedMessage) msgs.get(0); assertEquals("Messages match", message.getText(), new SourceTransformer().contentToString(msg)); // Wait for DONE status Thread.sleep(50); }
From source file:org.apache.servicemix.jms.JmsMarshalerTest.java
public void testEncoding() throws Exception { JmsEndpoint ep = new JmsEndpoint(); ep.setService(ReceiverComponent.SERVICE); ep.setEndpoint("jms"); ep.setTargetService(ReceiverComponent.SERVICE); ep.setTargetEndpoint(ReceiverComponent.ENDPOINT); ep.setRole(MessageExchange.Role.CONSUMER); ep.setDestinationStyle(AbstractJmsProcessor.STYLE_QUEUE); ep.setDestination(queue);//from w ww . j av a 2 s.c om ep.setDefaultMep(JbiConstants.IN_ONLY); ep.setMarshaler(new DefaultJmsMarshaler(ep)); QueueConnection qConn = connectionFactory.createQueueConnection(); QueueSession qSess = qConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); // Test character encoding. String defaultCharset = SourceTransformer.getDefaultCharset(); try { SourceTransformer.setDefaultCharset("ISO-8859-1"); SourceTransformer sourceTransformer = new SourceTransformer(); SoapMarshaler marshaler = new SoapMarshaler(true); SoapMessage soapMessage = marshaler.createReader() .read(getClass().getResourceAsStream("charsettest.xml")); soapMessage.setHeaders(null); soapMessage.setBodyName(null); soapMessage.setEnvelopeName(null); soapMessage.setSource(sourceTransformer.toDOMSource(soapMessage.getSource())); TextMessage m = (TextMessage) ep.getMarshaler().toJMS(soapMessage, null, qSess); assertEquals("Messages match", new SourceTransformer().toString(soapMessage.getSource()), m.getText().replace('\'', '"')); } finally { SourceTransformer.setDefaultCharset(defaultCharset); } }
From source file:org.easybatch.jms.JmsIntegrationTest.java
@Test public void testJmsRecordWriter() throws Exception { Context jndiContext = getJndiContext(); Queue queue = (Queue) jndiContext.lookup("q"); QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext .lookup("QueueConnectionFactory"); QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queueConnection.start();/* w ww .ja va 2 s .com*/ String dataSource = "foo" + LINE_SEPARATOR + "bar"; aNewJob().reader(new StringRecordReader(dataSource)).processor(new JmsMessageTransformer(queueSession)) .writer(new JmsQueueRecordWriter(queueConnectionFactory, queue)).call(); // Assert that queue contains 2 messages: "foo" and "bar" QueueBrowser queueBrowser = queueSession.createBrowser(queue); Enumeration enumeration = queueBrowser.getEnumeration(); assertThat(enumeration.hasMoreElements()).isTrue(); TextMessage message1 = (TextMessage) enumeration.nextElement(); assertThat(message1.getText()).isEqualTo("foo"); assertThat(enumeration.hasMoreElements()).isTrue(); TextMessage message2 = (TextMessage) enumeration.nextElement(); assertThat(message2.getText()).isEqualTo("bar"); assertThat(enumeration.hasMoreElements()).isFalse(); queueSession.close(); queueConnection.close(); }
From source file:org.wso2.carbon.oc.agent.publisher.mb.MBPublisher.java
/** * @param queueName - String mb queue name * @param jsonMessage - String mb queue message json string *//*from w w w. j av a 2 s . c o m*/ public void sendMessages(String queueName, String jsonMessage) { try { Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF); properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(username, password)); properties.put(QUEUE_NAME_PREFIX + queueName, queueName); InitialContext ctx = new InitialContext(properties); // lookup connection factory QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME); QueueConnection queueConnection = connFactory.createQueueConnection(); queueConnection.start(); QueueSession queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); // send message Queue queue = (Queue) ctx.lookup(queueName); // create the message to send TextMessage textMessage = queueSession.createTextMessage(jsonMessage); QueueSender queueSender = queueSession.createSender(queue); queueSender.send(textMessage); queueSender.close(); queueSession.close(); queueConnection.close(); } catch (JMSException e) { logger.error("MBPublisher connection down", e); } catch (NamingException e) { logger.error("Naming error", e); } }
From source file:org.easybatch.jms.JmsIntegrationTest.java
@Test public void testJmsSupport() throws Exception { Context jndiContext = getJndiContext(); QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext .lookup("QueueConnectionFactory"); Queue queue = (Queue) jndiContext.lookup("q"); QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(); QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); QueueSender queueSender = queueSession.createSender(queue); queueConnection.start();/*from ww w . j a va 2 s . c o m*/ //send a regular message to the queue TextMessage message = queueSession.createTextMessage(); message.setText(MESSAGE_TEXT); queueSender.send(message); //send a poison record to the queue queueSender.send(new JmsPoisonMessage()); Job job = aNewJob().reader(new JmsQueueRecordReader(queueConnectionFactory, queue)) .filter(new JmsPoisonRecordFilter()).processor(new RecordCollector()) .jobListener(new JmsQueueSessionListener(queueSession)) .jobListener(new JmsQueueConnectionListener(queueConnection)).build(); JobReport jobReport = JobExecutor.execute(job); assertThat(jobReport).isNotNull(); assertThat(jobReport.getParameters().getDataSource()).isEqualTo(EXPECTED_DATA_SOURCE_NAME); assertThat(jobReport.getMetrics().getTotalCount()).isEqualTo(2); assertThat(jobReport.getMetrics().getFilteredCount()).isEqualTo(1); assertThat(jobReport.getMetrics().getSuccessCount()).isEqualTo(1); List<JmsRecord> records = (List<JmsRecord>) jobReport.getResult(); assertThat(records).isNotNull().isNotEmpty().hasSize(1); JmsRecord jmsRecord = records.get(0); Header header = jmsRecord.getHeader(); assertThat(header).isNotNull(); assertThat(header.getNumber()).isEqualTo(1); assertThat(header.getSource()).isEqualTo(EXPECTED_DATA_SOURCE_NAME); Message payload = jmsRecord.getPayload(); assertThat(payload).isNotNull().isInstanceOf(TextMessage.class); TextMessage textMessage = (TextMessage) payload; assertThat(textMessage.getText()).isNotNull().isEqualTo(MESSAGE_TEXT); }
From source file:org.dawnsci.commandserver.core.consumer.RemoteSubmission.java
/** * Monitors a given bean in the status queue. * If the bean is not there throws exception. * If the bean is in a final state, returns the bean straight away. * /*from w w w . j a v a 2 s.co m*/ * Polls the queue for the unique id of the bean we want until it * encounters a final state of that bean. * * Polling rate is less than 1s * * NOTE this class can poll forever if the job it is looking at never finishes. * * @param obean * @param string * @return the bean once it is in a final state. * @throws exception if broker or queue absent */ public StatusBean monitor(StatusBean obean) throws Exception { if (getQueueName() == null || "".equals(getQueueName())) throw new Exception("Please specify a queue name!"); QueueConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri); QueueConnection 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); Class clazz = obean.getClass(); ObjectMapper mapper = new ObjectMapper(); try { POLL: while (true) { Thread.sleep(500); @SuppressWarnings("rawtypes") Enumeration e = qb.getEnumeration(); while (e.hasMoreElements()) { // We must final the bean somewhere. Message m = (Message) e.nextElement(); if (m == null) continue; if (m instanceof TextMessage) { TextMessage t = (TextMessage) m; final StatusBean bean = mapper.readValue(t.getText(), clazz); if (bean.getUniqueId().equals(obean.getUniqueId())) { if (bean.getStatus().isFinal()) return bean; System.out.println(bean.getPercentComplete()); continue POLL; } } } throw new Exception( "The bean with id " + obean.getUniqueId() + " does not exist in " + getQueueName() + "!"); } } finally { qCon.close(); } }
From source file:org.wso2.carbon.sample.consumer.QueueConsumer.java
public void run() { // create queue connection QueueConnection queueConnection = null; try {// w ww . ja va2 s . c om queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start(); } catch (JMSException e) { log.error("Can not create queue connection." + e.getMessage(), e); return; } Session session = null; try { session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages"); while (active) { Message message = consumer.receive(1000); if (message != null) { if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; Map<String, Object> map = new HashMap<String, Object>(); Enumeration enumeration = mapMessage.getMapNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); map.put(key, mapMessage.getObject(key)); } log.info("Received Map Message : " + map); } else if (message instanceof TextMessage) { log.info("Received Text Message : " + ((TextMessage) message).getText()); } else { log.info("Received message : " + message.toString()); } } } log.info("Finished listening for messages."); session.close(); queueConnection.stop(); queueConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }
From source file:org.wso2.siddhi.extension.output.transport.jms.util.QueueConsumer.java
public void run() { // create queue connection QueueConnection queueConnection = null; try {// w w w . j a v a 2 s . com queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start(); } catch (JMSException e) { log.error("Can not create queue connection." + e.getMessage(), e); return; } Session session; try { session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages at " + queueName); while (active) { Message message = consumer.receive(1000); if (message != null) { if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; Map<String, Object> map = new HashMap<String, Object>(); Enumeration enumeration = mapMessage.getMapNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); map.put(key, mapMessage.getObject(key)); } log.info("Received Map Message: " + map); } else if (message instanceof TextMessage) { log.info("Received Text Message: " + ((TextMessage) message).getText()); } else { log.info("Received message: " + message.toString()); } } } log.info("Finished listening for messages."); session.close(); queueConnection.stop(); queueConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }
From source file:org.wso2.extension.siddhi.io.jms.sink.util.QueueConsumer.java
public void run() { // create queue connection QueueConnection queueConnection = null; try {/*from ww w . j av a 2 s .c om*/ queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start(); } catch (JMSException e) { log.error("Can not create queue connection." + e.getMessage(), e); return; } Session session; try { session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages at " + queueName); while (active) { Message message = consumer.receive(1000); if (message != null) { resultContainer.eventReceived(message); if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; Map<String, Object> map = new HashMap<String, Object>(); Enumeration enumeration = mapMessage.getMapNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); map.put(key, mapMessage.getObject(key)); } log.info("Received Map Message: " + map); } else if (message instanceof TextMessage) { log.info("Received Text Message: " + ((TextMessage) message).getText()); } else { log.info("Received message: " + message.toString()); } } } log.info("Finished listening for messages."); session.close(); queueConnection.stop(); queueConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }
From source file:org.wso2.carbon.sample.consumer.JMSQueueMessageConsumer.java
public void run() { // create queue connection QueueConnection queueConnection = null; try {//w w w. j a v a 2 s .c om queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start(); } catch (JMSException e) { log.info("Can not create queue connection." + e); return; } Session session = null; try { session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(destination); int count = 0; long totalLatency = 0; long lastTimestamp = System.currentTimeMillis(); while (active) { Message message = consumer.receive(1000); if (message != null) { // if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; long currentTime = System.currentTimeMillis(); long sentTimestamp = (Long) mapMessage.getObject("time"); totalLatency = totalLatency + (currentTime - sentTimestamp); int logCount = 1000; if ((count % logCount == 0) && (count > warmUpCount)) { double rate = (logCount * 1000.0d / (System.currentTimeMillis() - lastTimestamp)); log.info("Consumer: " + consumerId + " (" + logCount + " received) rate: " + rate + " Latency:" + (totalLatency / (logCount * 1.0d))); // log.info("total latency:" + totalLatency); log.info("Total rate: " + (int) (consumers * rate)); totalLatency = 0; lastTimestamp = System.currentTimeMillis(); } count++; } } log.info("Finished listening for messages."); } catch (JMSException e) { log.info("Can not subscribe." + e); } finally { if (session != null) { try { session.close(); } catch (JMSException e) { log.error(e); } } try { queueConnection.stop(); queueConnection.close(); } catch (JMSException e) { e.printStackTrace(); } } }