List of usage examples for javax.jms ConnectionFactory createConnection
Connection createConnection(String userName, String password) throws JMSException;
From source file:org.openanzo.combus.bayeux.BridgeConnectionManager.java
/** * Creates a single JMS connection and session for use by the BayeuxJMSBridge. It connects to the combus using a configured sysadmin account. * /* w w w . j a va2 s.co m*/ * @param factory * this will be used to create the JMS connection and session. * @param properties * must contain the username and password * @throws JMSException */ protected void initialize(ConnectionFactory factory, Properties properties) throws AnzoException { try { conn = factory.createConnection(credentials.getUserName(), credentials.getPassword()); conn.setExceptionListener(new ExceptionListener() { public void onException(JMSException exception) { if (!closed) { // if user has not requested disconnect if (exception.getCause() instanceof BrokerStoppedException || exception.getCause() instanceof TransportDisposedIOException) { closed = true; if (conn != null) { try { conn.close(); } catch (JMSException e) { log.debug(LogUtils.COMBUS_MARKER, "Error closing JMS connection", e); } } } else { log.error(LogUtils.COMBUS_MARKER, "Exception over Bayeux JMS connection", exception); } } } }); conn.start(); session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); mp = session.createProducer(null); mp.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // setup all the destination queues destinations.put(COMBUS.NOTIFICATION_SERVICE_QUEUE, session.createQueue(COMBUS.NOTIFICATION_SERVICE_QUEUE)); destinations.put(COMBUS.MODEL_SERVICE_QUEUE, session.createQueue(COMBUS.MODEL_SERVICE_QUEUE)); destinations.put(COMBUS.UPDATE_SERVICE_QUEUE, session.createQueue(COMBUS.UPDATE_SERVICE_QUEUE)); destinations.put(COMBUS.AUTHENTICATION_SERVICE_QUEUE, session.createQueue(COMBUS.AUTHENTICATION_SERVICE_QUEUE)); destinations.put(COMBUS.REPLICATION_SERVICE_QUEUE, session.createQueue(COMBUS.REPLICATION_SERVICE_QUEUE)); destinations.put(COMBUS.QUERY_SERVICE_QUEUE, session.createQueue(COMBUS.QUERY_SERVICE_QUEUE)); destinations.put(COMBUS.RESET_SERVICE_QUEUE, session.createQueue(COMBUS.RESET_SERVICE_QUEUE)); destinations.put(COMBUS.EXECUTION_SERVICE_QUEUE, session.createQueue(COMBUS.EXECUTION_SERVICE_QUEUE)); destinations.put(COMBUS.AUTHORIZATION_SERVICE_QUEUE, session.createQueue(COMBUS.AUTHORIZATION_SERVICE_QUEUE)); } catch (JMSException jmsex) { throw new AnzoException(ExceptionConstants.COMBUS.JMS_CONNECT_FAILED, jmsex); } }
From source file:org.openanzo.combus.CombusConnection.java
private void performConnect() throws JMSException, AnzoException { if (connected || closing) { return;/* ww w .j ava 2 s . c o m*/ } if (jmsProvider == null) { throw new AnzoException(ExceptionConstants.COMBUS.NOTIFICATION_SERVICE_ERROR); } Properties propertiesNew = new Properties(); CombusProperties.setHost(propertiesNew, host); CombusProperties.setPort(propertiesNew, port); CombusProperties.setUseSsl(propertiesNew, useSsl); ConnectionFactory connectionFactory = jmsProvider.createConnectionFactory(propertiesNew); if (connectionFactory != null) { if (userName != null && password != null) { connection = connectionFactory.createConnection(userName, password); } else { connection = connectionFactory.createConnection(); } connection.setExceptionListener(new ExceptionListener() { public void onException(JMSException exception) { if (!closed) { // if user has not requested disconnect if (exception.getCause() instanceof BrokerStoppedException || exception.getCause() instanceof TransportDisposedIOException) { closed = true; } else { try { fireConnectionStateChange(INotificationConnectionListener.CONNECTIONFAILED); performDisconnect(false); } catch (AnzoException e) { log.error(LogUtils.COMBUS_MARKER, Messages.formatString(ExceptionConstants.COMBUS.JMS_DISCONNECT_FAILED), e); } finally { connected = false; } } } } }); connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); destinations.clear(); destinations.put(INotificationRegistrationService.SERVICE_NAME, session.createQueue(COMBUS.NOTIFICATION_SERVICE_QUEUE)); destinations.put(IModelService.SERVICE_NAME, session.createQueue(COMBUS.MODEL_SERVICE_QUEUE)); destinations.put(IAuthorizationService.SERVICE_NAME, session.createQueue(COMBUS.AUTHORIZATION_SERVICE_QUEUE)); destinations.put(IAuthenticationService.SERVICE_NAME, session.createQueue(COMBUS.AUTHENTICATION_SERVICE_QUEUE)); destinations.put(IReplicationService.SERVICE_NAME, session.createQueue(COMBUS.REPLICATION_SERVICE_QUEUE)); destinations.put(IResetService.SERVICE_NAME, session.createQueue(COMBUS.RESET_SERVICE_QUEUE)); destinations.put(IUpdateService.SERVICE_NAME, session.createQueue(COMBUS.UPDATE_SERVICE_QUEUE)); destinations.put(IQueryService.SERVICE_NAME, session.createQueue(COMBUS.QUERY_SERVICE_QUEUE)); destinations.put(IIndexService.SERVICE_NAME, session.createQueue(COMBUS.INDEX_SERVICE_QUEUE)); destinations.put(IExecutionService.SERVICE_NAME, session.createQueue(COMBUS.EXECUTION_SERVICE_QUEUE)); tempQueue = session.createTemporaryQueue(); messageProducer = session.createProducer(null); messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); messageConsumer = session.createConsumer(tempQueue); listener = new JMSMessageListener(); messageConsumer.setMessageListener(listener); connected = true; fireConnectionStateChange(INotificationConnectionListener.CONNECTED); } return; }
From source file:com.mirth.connect.connectors.jms.JmsClient.java
/** * Starts a JMS connection and session./*from ww w . ja v a 2 s . c om*/ */ public void start() throws ConnectorTaskException { final String channelId = connector.getChannelId(); String channelName = connector.getChannel().getName(); Map<String, String> connectionProperties = replacer .replaceValues(connectorProperties.getConnectionProperties(), channelId, channelName); ConnectionFactory connectionFactory = null; if (connectorProperties.isUseJndi()) { try { connectionFactory = lookupConnectionFactoryWithJndi(); } catch (Exception e) { throw new ConnectorTaskException("Failed to obtain the connection factory via JNDI", e); } } else { String className = replacer.replaceValues(connectorProperties.getConnectionFactoryClass(), channelId, channelName); try { MirthContextFactory contextFactory = contextFactoryController.getContextFactory(resourceIds); connectionFactory = (ConnectionFactory) Class .forName(className, true, contextFactory.getApplicationClassLoader()).newInstance(); } catch (Exception e) { throw new ConnectorTaskException("Failed to instantiate ConnectionFactory class: " + className, e); } } BeanUtil.setProperties(connectionFactory, connectionProperties); try { logger.debug("Creating JMS connection and session"); connection = connectionFactory.createConnection( replacer.replaceValues(connectorProperties.getUsername(), channelId, channelName), replacer.replaceValues(connectorProperties.getPassword(), channelId, channelName)); String clientId = replacer.replaceValues(connectorProperties.getClientId(), channelId, channelName); if (!clientId.isEmpty()) { connection.setClientID(clientId); } connection.setExceptionListener(this); logger.debug("Starting JMS connection"); connection.start(); logger.debug("Creating JMS session"); session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); logger.debug("JMS session created"); } catch (JMSException e) { try { stop(); } catch (Exception e1) { } throw new ConnectorTaskException("Failed to establish a JMS connection", e); } connected.set(true); }
From source file:com.mirth.connect.connectors.jms.JmsDispatcher.java
/** * Get the JmsConnection from the cache if one exists, otherwise a new one will be created. This * method is synchronized otherwise multiple threads may try to create the same connection * simultaneously. Only one thread is allowed to create a connection at a time. Subsequent * threads will then retrieve the connection that was already created. *//*ww w . j a v a2s. c om*/ private synchronized JmsConnection getJmsConnection(JmsDispatcherProperties jmsDispatcherProperties, String connectionKey, Long dispatcherId, boolean replace) throws Exception { // If the connection needs to be replaced, clean up the old connection and remove it from the cache. if (replace) { closeJmsConnectionQuietly(connectionKey); } JmsConnection jmsConnection = jmsConnections.get(connectionKey); if (jmsConnection == null) { if (jmsConnections.size() >= maxConnections) { throw new Exception("Cannot create new connection. Maximum number (" + maxConnections + ") of cached connections reached."); } Context initialContext = null; ConnectionFactory connectionFactory = null; Connection connection = null; Map<String, String> connectionProperties = jmsDispatcherProperties.getConnectionProperties(); if (jmsDispatcherProperties.isUseJndi()) { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { MirthContextFactory contextFactory = contextFactoryController .getContextFactory(getResourceIds()); Thread.currentThread().setContextClassLoader(contextFactory.getApplicationClassLoader()); Hashtable<String, Object> env = new Hashtable<String, Object>(); env.put(Context.PROVIDER_URL, jmsDispatcherProperties.getJndiProviderUrl()); env.put(Context.INITIAL_CONTEXT_FACTORY, jmsDispatcherProperties.getJndiInitialContextFactory()); env.put(Context.SECURITY_PRINCIPAL, jmsDispatcherProperties.getUsername()); env.put(Context.SECURITY_CREDENTIALS, jmsDispatcherProperties.getPassword()); initialContext = new InitialContext(env); String connectionFactoryName = jmsDispatcherProperties.getJndiConnectionFactoryName(); connectionFactory = (ConnectionFactory) initialContext.lookup(connectionFactoryName); } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); } } else { String className = jmsDispatcherProperties.getConnectionFactoryClass(); MirthContextFactory contextFactory = contextFactoryController.getContextFactory(getResourceIds()); connectionFactory = (ConnectionFactory) Class .forName(className, true, contextFactory.getApplicationClassLoader()).newInstance(); } BeanUtil.setProperties(connectionFactory, connectionProperties); try { logger.debug("Creating JMS connection and session"); connection = connectionFactory.createConnection(jmsDispatcherProperties.getUsername(), jmsDispatcherProperties.getPassword()); String clientId = jmsDispatcherProperties.getClientId(); if (!clientId.isEmpty()) { connection.setClientID(clientId); } logger.debug("Starting JMS connection"); connection.start(); } catch (JMSException e) { try { if (connection != null) { connection.close(); } } catch (Exception e1) { logger.debug("Failed to close JMS connection.", e); } try { if (initialContext != null) { initialContext.close(); } } catch (Exception e1) { logger.debug("Failed to close initial context.", e); } throw e; } // Create the new JmsConnection and add it to the cache. jmsConnection = new JmsConnection(connection, initialContext); jmsConnections.put(connectionKey, jmsConnection); } return jmsConnection; }