List of usage examples for javax.jms Connection close
void close() throws JMSException;
From source file:org.exist.messaging.JmsMessageSender.java
@Override public NodeImpl send(JmsMessagingConfiguration config, MessagingMetadata metadata, Item content) throws XPathException { // JMS specific checks config.validateContent();/*from ww w . j a v a 2 s .c o m*/ // Retrieve relevant values String initialContextFactory = config.getInitalContextProperty(Context.INITIAL_CONTEXT_FACTORY); String providerURL = config.getInitalContextProperty(Context.PROVIDER_URL); String connectionFactory = config.getConnectionFactory(); String destination = config.getDestination(); // TODO split up, use more exceptions, add better reporting try { Properties props = new Properties(); props.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); props.setProperty(Context.PROVIDER_URL, providerURL); javax.naming.Context context = new InitialContext(props); // Setup connection ConnectionFactory cf = (ConnectionFactory) context.lookup(connectionFactory); Connection connection = cf.createConnection(); // Lookup queue Destination dest = (Destination) context.lookup(destination); // Create session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create message producer MessageProducer producer = session.createProducer(dest); // Create message Message message = createMessage(session, content, metadata, xqcontext); // Write properties Map<String, String> kvs = metadata.getValueMap(); for (String key : kvs.keySet()) { message.setStringProperty(key, kvs.get(key)); } // Send message producer.send(message); // Close connection // TODO keep connection open for re-use, efficiency connection.close(); return createReport(message, xqcontext); } catch (Throwable ex) { LOG.error(ex); throw new XPathException(ex); } }
From source file:com.microsoft.azure.servicebus.samples.jmstopicquickstart.JmsTopicQuickstart.java
private void receiveFromSubscription(ConnectionStringBuilder csb, Context context, ConnectionFactory cf, String name) throws NamingException, JMSException, InterruptedException { AtomicInteger totalReceived = new AtomicInteger(0); System.out.printf("Subscription %s: \n", name); Destination subscription = (Destination) context.lookup(name); // Create Connection Connection connection = cf.createConnection(csb.getSasKeyName(), csb.getSasKey()); connection.start();// ww w.jav a2 s. co m // Create Session, no transaction, client ack Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); // Create consumer MessageConsumer consumer = session.createConsumer(subscription); // Set callback listener. Gets called for each received message. consumer.setMessageListener(message -> { try { System.out.printf("Received message %d with sq#: %s\n", totalReceived.incrementAndGet(), // increments the counter message.getJMSMessageID()); message.acknowledge(); } catch (Exception e) { System.out.printf("%s", e.toString()); } }); // wait on the main thread until all sent messages have been received while (totalReceived.get() < totalSend) { Thread.sleep(1000); } consumer.close(); session.close(); connection.stop(); connection.close(); }
From source file:org.apache.activemq.artemis.tests.integration.persistence.metrics.JournalPendingMessageTest.java
@Test public void testQueueLargeMessageSize() throws Exception { ActiveMQConnectionFactory acf = (ActiveMQConnectionFactory) cf; acf.setMinLargeMessageSize(1000);/*from w ww. jav a 2s .c o m*/ Connection connection = cf.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); String testText = StringUtils.repeat("t", 5000); ActiveMQTextMessage message = (ActiveMQTextMessage) session.createTextMessage(testText); session.createProducer(session.createQueue(defaultQueueName)).send(message); verifyPendingStats(defaultQueueName, 1, message.getCoreMessage().getPersistentSize()); verifyPendingDurableStats(defaultQueueName, 1, message.getCoreMessage().getPersistentSize()); connection.close(); this.killServer(); this.restartServer(); verifyPendingStats(defaultQueueName, 1, message.getCoreMessage().getPersistentSize()); verifyPendingDurableStats(defaultQueueName, 1, message.getCoreMessage().getPersistentSize()); }
From source file:org.rhq.enterprise.server.drift.DriftManagerBean.java
@Override @TransactionAttribute(REQUIRES_NEW)/* w w w . j av a 2 s. co m*/ public void addChangeSet(Subject subject, int resourceId, long zipSize, InputStream zipStream) throws Exception { Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(changesetQueue); ObjectMessage msg = session.createObjectMessage(new DriftUploadRequest(resourceId, zipSize, zipStream)); producer.send(msg); connection.close(); }
From source file:org.wso2.carbon.esb.scenario.test.common.jms.ActiveMQJMSClient.java
/** * Function to retrieve message from specified message queue * * @param queueName Name of the queue// ww w . java 2 s . c o m * @param timeout Timeout value (in milliseconds) * @return Retrieved message from the queue * @throws JMSException if error occurred */ public Message consumeMessageFromQueue(String queueName, long timeout) throws JMSException { Connection connection = null; Session session = null; MessageConsumer consumer = null; try { // Create a ConnectionFactory ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl); // Create a Connection connection = connectionFactory.createConnection(); connection.start(); connection.setExceptionListener(this); // Create a Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Create the destination (Topic or Queue) Destination destination = session.createQueue(queueName); // Create a MessageConsumer from the Session to the Topic or Queue consumer = session.createConsumer(destination); // Wait for a message return consumer.receive(timeout); } finally { if (consumer != null) { consumer.close(); } if (session != null) { session.close(); } if (connection != null) { connection.close(); } } }
From source file:org.apache.activemq.artemis.tests.integration.persistence.metrics.JournalPendingMessageTest.java
@Test public void testTopicNonPersistentMessageSize() throws Exception { AtomicLong publishedMessageSize = new AtomicLong(); Connection connection = cf.createConnection(); connection.setClientID("clientId"); connection.start();/*from w w w.j av a2 s . c o m*/ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(session.createTopic(defaultTopicName)); publishTestTopicMessages(200, DeliveryMode.NON_PERSISTENT, publishedMessageSize); verifyPendingStats(defaultTopicName, 200, publishedMessageSize.get()); // consume all messages consumeTestMessages(consumer, 200); // All messages should now be gone verifyPendingStats(defaultTopicName, 0, 0); connection.close(); }
From source file:org.ahp.core.messaging.AhpJmsProducer.java
public void sendTextMessage(String pTextMessage, AhpJmsDestinationTypes pAhpJmsDestinationTypes, AhpJmsDestinationNames pAhpJmsDestinationNames) { Connection lConnection = null; Session lSession = null;//from w w w .j ava 2 s. c o m try { lConnection = this.mJmsConnectionFactory.createConnection(); lSession = lConnection.createSession(true, Session.AUTO_ACKNOWLEDGE); Destination lDestination = null; if (pAhpJmsDestinationTypes.equals(AhpJmsDestinationTypes.Queue)) lDestination = lSession.createQueue(pAhpJmsDestinationNames.toString()); else lDestination = lSession.createTopic(pAhpJmsDestinationNames.toString()); MessageProducer lMessageProducer = lSession.createProducer(lDestination); lMessageProducer.setDeliveryMode(DeliveryMode.PERSISTENT); //lMessageProducer.setTimeToLive( timeToLive ); Message lTextMessage = lSession.createTextMessage(pTextMessage); LOGGER.debug("AhpJmsProducer sending message to Destination " + pAhpJmsDestinationNames.toString() + " TextMessage :: \n" + pTextMessage); lMessageProducer.send(lTextMessage); lSession.commit(); } catch (JMSException exJms) { LOGGER.error("", exJms); } finally { try { lConnection.close(); } catch (JMSException exJms) { LOGGER.error("", exJms); } } }
From source file:org.rhq.enterprise.server.drift.DriftManagerBean.java
@Override @TransactionAttribute(REQUIRES_NEW)/* ww w . java 2 s . co m*/ public void addFiles(Subject subject, int resourceId, String driftDefName, String token, long zipSize, InputStream zipStream) throws Exception { Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(fileQueue); ObjectMessage msg = session .createObjectMessage(new DriftUploadRequest(resourceId, driftDefName, token, zipSize, zipStream)); producer.send(msg); connection.close(); }
From source file:com.mothsoft.alexis.engine.retrieval.DocumentRetrievalTaskImpl.java
private String requestParse(final Long documentId, final String content) { Connection connection = null; Session session = null;/*from w ww . ja va 2 s.c om*/ MessageProducer producer = null; // set up JMS connection, session, consumer, producer try { connection = this.connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); producer = session.createProducer(this.requestQueue); logger.info("Sending parse request, document ID: " + documentId); final TextMessage textMessage = session.createTextMessage(content); textMessage.setJMSReplyTo(this.responseQueue); textMessage.setLongProperty(DOCUMENT_ID, documentId); producer.send(textMessage); } catch (JMSException e) { throw new RuntimeException(e); } finally { try { if (producer != null) { producer.close(); } if (session != null) { session.close(); } if (connection != null) { connection.close(); } } catch (JMSException e) { throw new RuntimeException(e); } } return content; }
From source file:org.apache.falcon.regression.core.supportClasses.JmsMessageConsumer.java
@Override public void run() { try {// www . jav a 2s.c o m // Getting JMS connection from the server Connection connection = new ActiveMQConnectionFactory(brokerURL).createConnection(); connection.start(); // Creating session for sending messages Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic(topicName); MessageConsumer consumer = session.createConsumer(destination); try { LOGGER.info("Starting to receive messages."); int count = 0; for (; count < MAX_MESSAGE_COUNT; ++count) { Message message = consumer.receive(); //blocking call if (message == null) { LOGGER.info("Received empty message, count = " + count); } else { LOGGER.info("Received message, id = " + message.getJMSMessageID()); receivedMessages.add((MapMessage) message); } } if (count >= MAX_MESSAGE_COUNT) { LOGGER.warn("Not reading more messages, already read " + count + " messages."); } } finally { LOGGER.info("Stopping to receive messages."); connection.close(); } } catch (Exception e) { LOGGER.info("caught exception: " + ExceptionUtils.getStackTrace(e)); } }