List of usage examples for javax.jms Connection createSession
Session createSession(boolean transacted, int acknowledgeMode) throws JMSException;
From source file:org.seedstack.seed.ws.internal.jms.WSJmsPlugin.java
@Override public InitState init(InitContext initContext) { Configuration wsConfiguration = null; for (Plugin plugin : initContext.pluginsRequired()) { if (plugin instanceof JmsPlugin) { jmsPlugin = (JmsPlugin) plugin; }/*from w w w . j a v a 2 s . c om*/ if (plugin instanceof WSPlugin) { wsPlugin = (WSPlugin) plugin; } if (plugin instanceof ApplicationPlugin) { wsConfiguration = ((ApplicationPlugin) plugin).getApplication().getConfiguration() .subset(WS_CONFIGURATION_PREFIX); } } if (wsConfiguration == null) { throw new PluginException("Missing required application plugin"); } int cacheSize = wsConfiguration.getInt("jms.transport-cache.max-size", DEFAULT_CACHE_SIZE); final Configuration finalWsConfiguration = wsConfiguration; connectionCache = CacheBuilder.newBuilder().maximumSize(cacheSize) .concurrencyLevel(wsConfiguration.getInt("transport-cache.concurrency", DEFAULT_CACHE_CONCURRENCY)) .initialCapacity(wsConfiguration.getInt("transport-cache.initial-size", cacheSize / 4)) .build(new CacheLoader<SoapJmsUri, Connection>() { private AtomicInteger atomicInteger = new AtomicInteger(0); @Override public Connection load(SoapJmsUri soapJmsUri) throws NamingException, JMSException { String lookupVariant = soapJmsUri.getLookupVariant(); JmsFactory jmsFactory = jmsPlugin.getJmsFactory(); Connection connection; if (SoapJmsUri.JNDI_LOOKUP_VARIANT.equals(lookupVariant)) { String jndiConnectionFactoryName = soapJmsUri.getParameter("jndiConnectionFactoryName"); if (StringUtils.isBlank(jndiConnectionFactoryName)) { throw new IllegalArgumentException( "Missing jndiConnectionFactoryName parameter for JMS URI " + soapJmsUri.toString()); } String connectionName = soapJmsUri.getConnectionName(); if (connectionName == null) { connectionName = String.format(ANONYMOUS_CONNECTION_PATTERN, atomicInteger.getAndIncrement()); } ConnectionDefinition connectionDefinition = jmsFactory.createConnectionDefinition( connectionName, soapJmsUri.getConfiguration(finalWsConfiguration), (ConnectionFactory) SoapJmsUri.getContext(soapJmsUri) .lookup(jndiConnectionFactoryName)); connection = jmsFactory.createConnection(connectionDefinition); jmsPlugin.registerConnection(connection, connectionDefinition); } else if (SoapJmsUri.SEED_QUEUE_LOOKUP_VARIANT.equals(lookupVariant) || SoapJmsUri.SEED_TOPIC_LOOKUP_VARIANT.equals(lookupVariant)) { String connectionName = soapJmsUri.getConnectionName(); if (StringUtils.isBlank(connectionName)) { throw new IllegalArgumentException( "Missing connectionName parameter for JMS URI " + soapJmsUri.toString()); } connection = jmsPlugin.getConnection(connectionName); } else { throw new IllegalArgumentException("Unsupported lookup variant " + lookupVariant + " for JMS URI " + soapJmsUri.toString()); } if (connection == null) { throw new PluginException( "Unable to resolve connection for JMS URI " + soapJmsUri.toString()); } return connection; } }); for (Map.Entry<String, EndpointDefinition> endpointEntry : wsPlugin .getEndpointDefinitions(SUPPORTED_BINDINGS).entrySet()) { EndpointDefinition endpointDefinition = endpointEntry.getValue(); String endpointName = endpointEntry.getKey(); String serviceName = endpointDefinition.getServiceName().getLocalPart(); String portName = endpointDefinition.getPortName().getLocalPart(); String serviceNameAndServicePort = serviceName + "-" + portName; SoapJmsUri uri; try { uri = SoapJmsUri.parse(new URI(endpointDefinition.getUrl())); uri.setEndpointName(endpointName); } catch (URISyntaxException e) { throw new PluginException("Unable to parse endpoint URI", e); } Configuration endpointConfiguration = uri.getConfiguration(wsConfiguration); Connection connection; try { connection = connectionCache.get(uri); } catch (Exception e) { throw new PluginException("Unable to create JMS connection for WS " + serviceNameAndServicePort, e); } Session session; try { session = connection.createSession(endpointConfiguration.getBoolean("transactional", true), Session.AUTO_ACKNOWLEDGE); } catch (JMSException e) { throw new PluginException("Unable to create JMS session for WS " + serviceNameAndServicePort, e); } Destination destination; try { destination = SoapJmsUri.getDestination(uri, session); } catch (Exception e) { throw new PluginException("Unable to create JMS destination for WS " + serviceNameAndServicePort, e); } WSJmsMessageListener messageListener = new WSJmsMessageListener(uri, new JmsAdapter(wsPlugin.createWSEndpoint(endpointDefinition, null)), session); String messageListenerName = String.format(LISTENER_NAME_PATTERN, endpointName); try { Class<? extends MessagePoller> poller = getPoller(endpointConfiguration); jmsPlugin.registerMessageListener( new MessageListenerInstanceDefinition(messageListenerName, uri.getConnectionName(), session, destination, endpointConfiguration.getString("selector"), messageListener, poller)); } catch (Exception e) { throw SeedException.wrap(e, WSJmsErrorCodes.UNABLE_TO_REGISTER_MESSAGE_LISTENER) .put("messageListenerName", messageListenerName); } wsJmsMessageListeners.add(messageListener); } return InitState.INITIALIZED; }
From source file:org.seedstack.ws.jms.internal.WSJmsPlugin.java
@Override public InitState initialize(InitContext initContext) { jmsPlugin = initContext.dependency(JmsPlugin.class); WSPlugin wsPlugin = initContext.dependency(WSPlugin.class); final WebServicesJmsConfig webServicesJmsConfig = getConfiguration(WebServicesJmsConfig.class); WebServicesJmsConfig.JmsConfig.CacheConfig cacheConfig = webServicesJmsConfig.jms().connectionCache(); int cacheSize = cacheConfig.getMaxSize(); connectionCache = CacheBuilder.newBuilder().maximumSize(cacheSize) .concurrencyLevel(cacheConfig.getConcurrencyLevel()).initialCapacity(cacheConfig.getInitialSize()) .build(new CacheLoader<SoapJmsUri, Connection>() { private AtomicInteger atomicInteger = new AtomicInteger(0); @Override/* w w w . j a va 2s. c om*/ public Connection load(SoapJmsUri soapJmsUri) throws NamingException, JMSException { String lookupVariant = soapJmsUri.getLookupVariant(); JmsFactory jmsFactory = jmsPlugin.getJmsFactory(); Connection connection; if (SoapJmsUri.JNDI_LOOKUP_VARIANT.equals(lookupVariant)) { String jndiConnectionFactoryName = soapJmsUri.getParameter("jndiConnectionFactoryName"); if (StringUtils.isBlank(jndiConnectionFactoryName)) { throw new IllegalArgumentException( "Missing jndiConnectionFactoryName parameter for JMS URI " + soapJmsUri.toString()); } String connectionName = soapJmsUri.getConnectionName(); if (connectionName == null) { connectionName = String.format(ANONYMOUS_CONNECTION_PATTERN, atomicInteger.getAndIncrement()); } ConnectionDefinition connectionDefinition = jmsFactory.createConnectionDefinition( connectionName, soapJmsUri.getConfiguration(webServicesJmsConfig).jms().getConnection(), (ConnectionFactory) SoapJmsUri.getContext(soapJmsUri) .lookup(jndiConnectionFactoryName)); connection = jmsFactory.createConnection(connectionDefinition); jmsPlugin.registerConnection(connection, connectionDefinition); } else if (SoapJmsUri.SEED_QUEUE_LOOKUP_VARIANT.equals(lookupVariant) || SoapJmsUri.SEED_TOPIC_LOOKUP_VARIANT.equals(lookupVariant)) { String connectionName = soapJmsUri.getConnectionName(); if (StringUtils.isBlank(connectionName)) { throw new IllegalArgumentException( "Missing connectionName parameter for JMS URI " + soapJmsUri.toString()); } connection = jmsPlugin.getConnection(connectionName); } else { throw new IllegalArgumentException("Unsupported lookup variant " + lookupVariant + " for JMS URI " + soapJmsUri.toString()); } if (connection == null) { throw new PluginException( "Unable to resolve connection for JMS URI " + soapJmsUri.toString()); } return connection; } }); for (Map.Entry<String, EndpointDefinition> endpointEntry : wsPlugin .getEndpointDefinitions(SUPPORTED_BINDINGS).entrySet()) { EndpointDefinition endpointDefinition = endpointEntry.getValue(); String endpointName = endpointEntry.getKey(); String serviceName = endpointDefinition.getServiceName().getLocalPart(); String portName = endpointDefinition.getPortName().getLocalPart(); String serviceNameAndServicePort = serviceName + "-" + portName; SoapJmsUri uri; try { uri = SoapJmsUri.parse(new URI(endpointDefinition.getUrl())); uri.setEndpointName(endpointName); } catch (URISyntaxException e) { throw new PluginException("Unable to parse endpoint URI", e); } WebServicesJmsConfig.JmsEndpointConfig endpointConfiguration = uri .getConfiguration(webServicesJmsConfig); Connection connection; try { connection = connectionCache.get(uri); } catch (Exception e) { throw new PluginException("Unable to create JMS connection for WS " + serviceNameAndServicePort, e); } Session session; try { session = connection.createSession(endpointConfiguration.jms().isTransactional(), Session.AUTO_ACKNOWLEDGE); } catch (JMSException e) { throw new PluginException("Unable to create JMS session for WS " + serviceNameAndServicePort, e); } Destination destination; try { destination = SoapJmsUri.getDestination(uri, session); } catch (Exception e) { throw new PluginException("Unable to create JMS destination for WS " + serviceNameAndServicePort, e); } WSJmsMessageListener messageListener = new WSJmsMessageListener(uri, new JmsAdapter(wsPlugin.createWSEndpoint(endpointDefinition, null)), session); String messageListenerName = String.format(LISTENER_NAME_PATTERN, endpointName); try { jmsPlugin.registerMessageListener(new MessageListenerInstanceDefinition(messageListenerName, uri.getConnectionName(), session, destination, endpointConfiguration.jms().getSelector(), messageListener, endpointConfiguration.jms().getMessagePoller())); } catch (Exception e) { throw SeedException.wrap(e, WSJmsErrorCodes.UNABLE_TO_REGISTER_MESSAGE_LISTENER) .put("messageListenerName", messageListenerName); } wsJmsMessageListeners.add(messageListener); } return InitState.INITIALIZED; }
From source file:nl.nn.adapterframework.extensions.tibco.SendTibcoMessage.java
public String doPipeWithTimeoutGuarded(Object input, IPipeLineSession session) throws PipeRunException { Connection connection = null; Session jSession = null;//from ww w .j a v a 2 s . c o m MessageProducer msgProducer = null; Destination destination = null; String url_work; String authAlias_work; String userName_work; String password_work; String queueName_work; String messageProtocol_work; int replyTimeout_work; String soapAction_work; String result = null; ParameterValueList pvl = null; if (getParameterList() != null) { ParameterResolutionContext prc = new ParameterResolutionContext((String) input, session); try { pvl = prc.getValues(getParameterList()); } catch (ParameterException e) { throw new PipeRunException(this, getLogPrefix(session) + "exception on extracting parameters", e); } } url_work = getParameterValue(pvl, "url"); if (url_work == null) { url_work = getUrl(); } authAlias_work = getParameterValue(pvl, "authAlias"); if (authAlias_work == null) { authAlias_work = getAuthAlias(); } userName_work = getParameterValue(pvl, "userName"); if (userName_work == null) { userName_work = getUserName(); } password_work = getParameterValue(pvl, "password"); if (password_work == null) { password_work = getPassword(); } queueName_work = getParameterValue(pvl, "queueName"); if (queueName_work == null) { queueName_work = getQueueName(); } messageProtocol_work = getParameterValue(pvl, "messageProtocol"); if (messageProtocol_work == null) { messageProtocol_work = getMessageProtocol(); } String replyTimeout_work_str = getParameterValue(pvl, "replyTimeout"); if (replyTimeout_work_str == null) { replyTimeout_work = getReplyTimeout(); } else { replyTimeout_work = Integer.parseInt(replyTimeout_work_str); } soapAction_work = getParameterValue(pvl, "soapAction"); if (soapAction_work == null) soapAction_work = getSoapAction(); if (StringUtils.isEmpty(soapAction_work) && !StringUtils.isEmpty(queueName_work)) { String[] q = queueName_work.split("\\."); if (q.length > 0) { if (q[0].equalsIgnoreCase("P2P") && q.length >= 4) { soapAction_work = q[3]; } else if (q[0].equalsIgnoreCase("ESB") && q.length == 8) { soapAction_work = q[5] + "_" + q[6]; } else if (q[0].equalsIgnoreCase("ESB") && q.length > 8) { soapAction_work = q[6] + "_" + q[7]; } } } if (StringUtils.isEmpty(soapAction_work)) { log.debug(getLogPrefix(session) + "deriving default soapAction"); try { URL resource = ClassUtils.getResourceURL(this, "/xml/xsl/esb/soapAction.xsl"); TransformerPool tp = new TransformerPool(resource, true); soapAction_work = tp.transform(input.toString(), null); } catch (Exception e) { log.error(getLogPrefix(session) + "failed to execute soapAction.xsl"); } } if (messageProtocol_work == null) { throw new PipeRunException(this, getLogPrefix(session) + "messageProtocol must be set"); } if (!messageProtocol_work.equalsIgnoreCase(REQUEST_REPLY) && !messageProtocol_work.equalsIgnoreCase(FIRE_AND_FORGET)) { throw new PipeRunException(this, getLogPrefix(session) + "illegal value for messageProtocol [" + messageProtocol_work + "], must be '" + REQUEST_REPLY + "' or '" + FIRE_AND_FORGET + "'"); } CredentialFactory cf = new CredentialFactory(authAlias_work, userName_work, password_work); try { TibjmsAdmin admin; try { admin = TibcoUtils.getActiveServerAdmin(url_work, cf); } catch (TibjmsAdminException e) { log.debug(getLogPrefix(session) + "caught exception", e); admin = null; } if (admin != null) { QueueInfo queueInfo; try { queueInfo = admin.getQueue(queueName_work); } catch (Exception e) { throw new PipeRunException(this, getLogPrefix(session) + " exception on getting queue info", e); } if (queueInfo == null) { throw new PipeRunException(this, getLogPrefix(session) + " queue [" + queueName_work + "] does not exist"); } try { admin.close(); } catch (TibjmsAdminException e) { log.warn(getLogPrefix(session) + "exception on closing Tibjms Admin", e); } } ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(url_work); connection = factory.createConnection(cf.getUsername(), cf.getPassword()); jSession = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); destination = jSession.createQueue(queueName_work); msgProducer = jSession.createProducer(destination); TextMessage msg = jSession.createTextMessage(); msg.setText(input.toString()); Destination replyQueue = null; if (messageProtocol_work.equalsIgnoreCase(REQUEST_REPLY)) { replyQueue = jSession.createTemporaryQueue(); msg.setJMSReplyTo(replyQueue); msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT); msgProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); msgProducer.setTimeToLive(replyTimeout_work); } else { msg.setJMSDeliveryMode(DeliveryMode.PERSISTENT); msgProducer.setDeliveryMode(DeliveryMode.PERSISTENT); } if (StringUtils.isNotEmpty(soapAction_work)) { log.debug( getLogPrefix(session) + "setting [SoapAction] property to value [" + soapAction_work + "]"); msg.setStringProperty("SoapAction", soapAction_work); } msgProducer.send(msg); if (log.isDebugEnabled()) { log.debug(getLogPrefix(session) + "sent message [" + msg.getText() + "] " + "to [" + msgProducer.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] " + "replyTo [" + msg.getJMSReplyTo() + "]"); } else { if (log.isInfoEnabled()) { log.info(getLogPrefix(session) + "sent message to [" + msgProducer.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] " + "replyTo [" + msg.getJMSReplyTo() + "]"); } } if (messageProtocol_work.equalsIgnoreCase(REQUEST_REPLY)) { String replyCorrelationId = msg.getJMSMessageID(); MessageConsumer msgConsumer = jSession.createConsumer(replyQueue, "JMSCorrelationID='" + replyCorrelationId + "'"); log.debug(getLogPrefix(session) + "] start waiting for reply on [" + replyQueue + "] selector [" + replyCorrelationId + "] for [" + replyTimeout_work + "] ms"); try { connection.start(); Message rawReplyMsg = msgConsumer.receive(replyTimeout_work); if (rawReplyMsg == null) { throw new PipeRunException(this, getLogPrefix(session) + "did not receive reply on [" + replyQueue + "] replyCorrelationId [" + replyCorrelationId + "] within [" + replyTimeout_work + "] ms"); } TextMessage replyMsg = (TextMessage) rawReplyMsg; result = replyMsg.getText(); } finally { } } else { result = msg.getJMSMessageID(); } } catch (JMSException e) { throw new PipeRunException(this, getLogPrefix(session) + " exception on sending message to Tibco queue", e); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { log.warn(getLogPrefix(session) + "exception on closing connection", e); } } } return result; }