List of usage examples for javax.jms Connection setClientID
void setClientID(String clientID) throws JMSException;
From source file:org.sakaiproject.kernel.messaging.JmsEmailMessageHandler.java
/** * {@inheritDoc}/*from w w w.j a va2 s. c o m*/ * * @see org.sakaiproject.kernel.api.messaging.MessageHandler#handle(java.lang.String, * java.lang.String, java.lang.String, javax.jcr.Node) */ public void handle(String userID, String filePath, String fileName, Node node) { try { InputStream inputStream = nodeFactory.getInputStream(filePath); String content = IOUtils.readFully(inputStream, "UTF-8"); Connection conn = connFactory.createConnection(); conn.setClientID("sakai.emailmessagehandler"); Session clientSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination emailTopic = clientSession.createTopic(emailQueueName); MessageProducer client = clientSession.createProducer(emailTopic); ObjectMessage mesg = clientSession.createObjectMessage(content); mesg.setJMSType(emailJmsType); client.send(mesg); } catch (JMSException e) { log.error(e.getMessage(), e); } catch (RepositoryException e) { log.error(e.getMessage(), e); } catch (JCRNodeFactoryServiceException e) { log.error(e.getMessage(), e); } catch (UnsupportedEncodingException e) { log.error(e.getMessage(), e); } catch (IOException e) { log.error(e.getMessage(), e); } }
From source file:org.seedstack.jms.internal.JmsFactoryImpl.java
Connection createRawConnection(ConnectionDefinition connectionDefinition) throws JMSException { Connection connection; if (connectionDefinition.getUser() != null) { connection = connectionDefinition.getConnectionFactory() .createConnection(connectionDefinition.getUser(), connectionDefinition.getPassword()); } else {//from w ww . j av a 2 s . c om connection = connectionDefinition.getConnectionFactory().createConnection(); } // client id is set here on raw connection if (connectionDefinition.isShouldSetClientId()) { LOGGER.debug("Setting client id as {} on connection {}", connectionDefinition.getClientId(), connectionDefinition.getName()); connection.setClientID(connectionDefinition.getClientId()); } return connection; }
From source file:org.springframework.jms.connection.SingleConnectionFactory.java
/** * Prepare the given Connection before it is exposed. * <p>The default implementation applies ExceptionListener and client id. * Can be overridden in subclasses./*from ww w . j a va 2 s . c o m*/ * @param con the Connection to prepare * @throws JMSException if thrown by JMS API methods * @see #setExceptionListener * @see #setReconnectOnException */ protected void prepareConnection(Connection con) throws JMSException { if (getClientId() != null) { con.setClientID(getClientId()); } if (this.aggregatedExceptionListener != null) { con.setExceptionListener(this.aggregatedExceptionListener); } else if (getExceptionListener() != null || isReconnectOnException()) { ExceptionListener listenerToUse = getExceptionListener(); if (isReconnectOnException()) { this.aggregatedExceptionListener = new AggregatedExceptionListener(); this.aggregatedExceptionListener.delegates.add(this); if (listenerToUse != null) { this.aggregatedExceptionListener.delegates.add(listenerToUse); } listenerToUse = this.aggregatedExceptionListener; } con.setExceptionListener(listenerToUse); } }
From source file:org.wso2.carbon.apimgt.jms.listener.utils.JMSUtils.java
/** * This is a JMS spec independent method to create a Connection. Please be cautious when * making any changes/*w w w .j a va2 s.c o m*/ * * @param conFac the ConnectionFactory to use * @param user optional user name * @param pass optional password * @param jmsSpec11 should we use JMS 1.1 API ? * @param isQueue is this to deal with a Queue? * @param isDurable whether the messaging provider is durable * @param clientID durable subscriber client id * @return a JMS Connection as requested * @throws JMSException on errors, to be handled and logged by the caller */ public static Connection createConnection(ConnectionFactory conFac, String user, String pass, boolean jmsSpec11, Boolean isQueue, boolean isDurable, String clientID) throws JMSException { Connection connection = null; if (log.isDebugEnabled()) { log.debug("Creating a " + (isQueue ? "Queue" : "Topic") + "Connection using credentials : (" + user + "/" + pass + ")"); } if (jmsSpec11 || isQueue == null) { if (user != null && pass != null) { connection = conFac.createConnection(user, pass); } else { connection = conFac.createConnection(); } if (isDurable) { connection.setClientID(clientID); } } else { QueueConnectionFactory qConFac = null; TopicConnectionFactory tConFac = null; if (isQueue) { qConFac = (QueueConnectionFactory) conFac; } else { tConFac = (TopicConnectionFactory) conFac; } if (user != null && pass != null) { if (qConFac != null) { connection = qConFac.createQueueConnection(user, pass); } else if (tConFac != null) { connection = tConFac.createTopicConnection(user, pass); } } else { if (qConFac != null) { connection = qConFac.createQueueConnection(); } else if (tConFac != null) { connection = tConFac.createTopicConnection(); } } if (isDurable) { connection.setClientID(clientID); } } return connection; }
From source file:org.wso2.carbon.inbound.endpoint.protocol.jms.factory.JMSConnectionFactory.java
public Connection createConnection() { if (connectionFactory == null) { logger.error("Connection cannot be establish to the broker. Please check the broker libs provided."); return null; }/*from w w w. j a v a 2 s . co m*/ Connection connection = null; try { if ("1.1".equals(jmsSpec)) { if (this.destinationType.equals(JMSConstants.JMSDestinationType.QUEUE)) { connection = ((QueueConnectionFactory) (this.connectionFactory)).createQueueConnection(); } else if (this.destinationType.equals(JMSConstants.JMSDestinationType.TOPIC)) { connection = ((TopicConnectionFactory) (this.connectionFactory)).createTopicConnection(); } if (isDurable) { connection.setClientID(clientId); } return connection; } else { QueueConnectionFactory qConFac = null; TopicConnectionFactory tConFac = null; if (this.destinationType.equals(JMSConstants.JMSDestinationType.QUEUE)) { qConFac = (QueueConnectionFactory) this.connectionFactory; } else { tConFac = (TopicConnectionFactory) this.connectionFactory; } if (qConFac != null) { connection = qConFac.createQueueConnection(); } else if (tConFac != null) { connection = tConFac.createTopicConnection(); } if (isDurable && !isSharedSubscription) { connection.setClientID(clientId); } return connection; } } catch (JMSException e) { logger.error("JMS Exception while creating connection through factory '" + this.connectionFactoryString + "' " + e.getMessage(), e); // Need to close the connection in the case if durable subscriptions if (connection != null) { try { connection.close(); } catch (Exception ex) { } } } return null; }
From source file:org.wso2.carbon.inbound.endpoint.protocol.jms.factory.JMSConnectionFactory.java
public Connection createConnection(String userName, String password) { Connection connection = null; try {/*from w w w.j a v a 2 s. c om*/ if (JMSConstants.JMS_SPEC_VERSION_1_1.equals(jmsSpec)) { if (this.destinationType.equals(JMSConstants.JMSDestinationType.QUEUE)) { connection = ((QueueConnectionFactory) (this.connectionFactory)).createQueueConnection(userName, password); } else if (this.destinationType.equals(JMSConstants.JMSDestinationType.TOPIC)) { connection = ((TopicConnectionFactory) (this.connectionFactory)).createTopicConnection(userName, password); } if (isDurable) { connection.setClientID(clientId); } return connection; } else { QueueConnectionFactory qConFac = null; TopicConnectionFactory tConFac = null; if (this.destinationType.equals(JMSConstants.JMSDestinationType.QUEUE)) { qConFac = (QueueConnectionFactory) this.connectionFactory; } else { tConFac = (TopicConnectionFactory) this.connectionFactory; } if (qConFac != null) { connection = qConFac.createQueueConnection(userName, password); } else if (tConFac != null) { connection = tConFac.createTopicConnection(userName, password); } if (isDurable && !isSharedSubscription) { connection.setClientID(clientId); } return connection; } } catch (JMSException e) { logger.error("JMS Exception while creating connection through factory '" + this.connectionFactoryString + "' " + e.getMessage()); // Need to close the connection in the case if durable subscriptions if (connection != null) { try { connection.close(); } catch (Exception ex) { } } } return null; }
From source file:tools.ConsumerTool.java
@Override public void run() { Connection connection = null; Session session = null;/*from ww w . ja v 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:tools.ProducerTool.java
@Override public void run() { Connection connection = null; Session session = null;//w ww. jav a 2 s . c o m try { connection = connectionFactory.createConnection(); if (clientId != null) { connection.setClientID(clientId); } 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); } } } MessageProducer producer = session.createProducer(destination); if (durable) { producer.setDeliveryMode(DeliveryMode.PERSISTENT); } else { producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); } int numMessagesToSend = useFinalControlMessage ? numMessages - 1 : numMessages; for (int i = 0; i < numMessagesToSend; i++) { String messageText = "Message " + i + " at " + new Date(); if (bytesLength > -1) { byte[] messageTextBytes = messageText.getBytes(StandardCharsets.UTF_8); BytesMessage bytesMessage = session.createBytesMessage(); bytesMessage.writeBytes(messageTextBytes); if (messageTextBytes.length < bytesLength) { byte[] paddingBytes = new byte[bytesLength - messageTextBytes.length]; bytesMessage.writeBytes(paddingBytes); } if (messageGroupId != null) { bytesMessage.setStringProperty("JMSXGroupID", messageGroupId); } LOGGER.info("Sending bytes message"); producer.send(bytesMessage); } else { TextMessage textMessage = session.createTextMessage(messageText); if (messageGroupId != null) { textMessage.setStringProperty("JMSXGroupID", messageGroupId); } LOGGER.info("Sending text message: " + messageText); producer.send(textMessage); } if (perMessageSleepMS > 0) { Thread.sleep(perMessageSleepMS); } if (transacted) { if ((i + 1) % batchSize == 0) { session.commit(); } } } if (useFinalControlMessage) { Message message = session.createMessage(); if (messageGroupId != null) { message.setStringProperty("JMSXGroupID", messageGroupId); } LOGGER.info("Sending message"); producer.send(message); if (transacted) { session.commit(); } } producer.close(); } catch (Exception ex) { LOGGER.error("ProducerTool 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 session", e); } } } }