/** Configures the jms broker url. */ public void setBrokerUrl() { String jmsBrokerHost = getJmsBrokerHost(); Integer jmsBrokerPort = getJmsBrokerPort(); connectionFactory.setBrokerURL("tcp://" + jmsBrokerHost + ":" + jmsBrokerPort); pooledConnectionFactory.setConnectionFactory(connectionFactory); configureTransportConnector(jmsBrokerHost, jmsBrokerPort); }
/** Configures the given connection factory */ public void configure(ActiveMQConnectionFactory factory, MessageActivationSpec activationSpec) { if (serverUrl != null) { factory.setBrokerURL(serverUrl); } if (clientid != null) { factory.setClientID(clientid); } if (redeliveryPolicy != null) { factory.setRedeliveryPolicy(redeliveryPolicy); } if (prefetchPolicy != null) { factory.setPrefetchPolicy(prefetchPolicy); } if (factory instanceof ActiveMQSslConnectionFactory) { String trustStore = defaultValue( activationSpec == null ? null : activationSpec.getTrustStore(), getTrustStore()); String trustStorePassword = defaultValue( activationSpec == null ? null : activationSpec.getTrustStorePassword(), getTrustStorePassword()); String keyStore = defaultValue(activationSpec == null ? null : activationSpec.getKeyStore(), getKeyStore()); String keyStorePassword = defaultValue( activationSpec == null ? null : activationSpec.getKeyStorePassword(), getKeyStorePassword()); String keyStoreKeyPassword = defaultValue( activationSpec == null ? null : activationSpec.getKeyStoreKeyPassword(), getKeyStoreKeyPassword()); ActiveMQSslConnectionFactory sslFactory = (ActiveMQSslConnectionFactory) factory; if (trustStore != null) { try { sslFactory.setTrustStore(trustStore); } catch (Exception e) { log.warn("Unable to set TrustStore", e); } } if (trustStorePassword != null) { sslFactory.setTrustStorePassword(trustStorePassword); } if (keyStore != null) { try { sslFactory.setKeyStore(keyStore); } catch (Exception e) { log.warn("Unable to set KeyStore", e); } } if (keyStorePassword != null) { sslFactory.setKeyStorePassword(keyStorePassword); } if (keyStoreKeyPassword != null) { sslFactory.setKeyStoreKeyPassword(keyStoreKeyPassword); } } }
@Bean public ConnectionFactory amqConnectionFactory() { CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(); ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(); activeMQConnectionFactory.setBrokerURL("vm://localhost?broker.persistent=false"); cachingConnectionFactory.setTargetConnectionFactory(activeMQConnectionFactory); cachingConnectionFactory.setSessionCacheSize(10); cachingConnectionFactory.setCacheProducers(true); cachingConnectionFactory.setReconnectOnException(true); return cachingConnectionFactory; }
public void testConnectionFactory() throws Exception { // Create sample connection factory ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(); factory.setDispatchAsync(true); factory.setBrokerURL("vm://test"); factory.setClientID("test"); factory.setCopyMessageOnSend(false); factory.setDisableTimeStampsByDefault(true); factory.setObjectMessageSerializationDefered(true); factory.setOptimizedMessageDispatch(false); factory.setPassword("pass"); factory.setUseAsyncSend(true); factory.setUseCompression(true); factory.setUseRetroactiveConsumer(true); factory.setUserName("user"); factory.getPrefetchPolicy().setQueuePrefetch(777); factory.getRedeliveryPolicy().setMaximumRedeliveries(15); factory.getRedeliveryPolicy().setBackOffMultiplier((short) 32); // Create reference Reference ref = JNDIReferenceFactory.createReference(factory.getClass().getName(), factory); // Get object created based on reference ActiveMQConnectionFactory temp; JNDIReferenceFactory refFactory = new JNDIReferenceFactory(); temp = (ActiveMQConnectionFactory) refFactory.getObjectInstance(ref, null, null, null); // Check settings assertEquals(factory.isDispatchAsync(), temp.isDispatchAsync()); assertEquals(factory.getBrokerURL(), temp.getBrokerURL()); assertEquals(factory.getClientID(), temp.getClientID()); assertEquals(factory.isCopyMessageOnSend(), temp.isCopyMessageOnSend()); assertEquals(factory.isDisableTimeStampsByDefault(), temp.isDisableTimeStampsByDefault()); assertEquals( factory.isObjectMessageSerializationDefered(), temp.isObjectMessageSerializationDefered()); assertEquals(factory.isOptimizedMessageDispatch(), temp.isOptimizedMessageDispatch()); assertEquals(factory.getPassword(), temp.getPassword()); assertEquals(factory.isUseAsyncSend(), temp.isUseAsyncSend()); assertEquals(factory.isUseCompression(), temp.isUseCompression()); assertEquals(factory.isUseRetroactiveConsumer(), temp.isUseRetroactiveConsumer()); assertEquals(factory.getUserName(), temp.getUserName()); assertEquals( factory.getPrefetchPolicy().getQueuePrefetch(), temp.getPrefetchPolicy().getQueuePrefetch()); assertEquals( factory.getRedeliveryPolicy().getMaximumRedeliveries(), temp.getRedeliveryPolicy().getMaximumRedeliveries()); assertEquals( factory.getRedeliveryPolicy().getBackOffMultiplier(), temp.getRedeliveryPolicy().getBackOffMultiplier()); }
// set up the jms queue void init(TournamentService host) { log.info("Server URL: " + serverUrl + ", queue: " + queueName); // Can't set this earlier, need the params from visualizerService ActiveMQConnectionFactory amqFactory = (ActiveMQConnectionFactory) connectionFactory.getTargetConnectionFactory(); amqFactory.setBrokerURL(serverUrl); // register host as listener container = new DefaultMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.setDestinationName(queueName); container.setMessageListener(host); container.setTaskExecutor(taskExecutor); container.afterPropertiesSet(); container.start(); connectionOpen = true; }
@Override protected CamelContext createCamelContext() throws Exception { SimpleRegistry registry = new SimpleRegistry(); registry.put("aggStrategy", AggregationStrategies.groupedExchange()); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL(broker.getTcpConnectorUri()); SjmsComponent sjmsComponent = new SjmsComponent(); sjmsComponent.setConnectionFactory(connectionFactory); SjmsBatchComponent sjmsBatchComponent = new SjmsBatchComponent(); sjmsBatchComponent.setConnectionFactory(connectionFactory); CamelContext context = new DefaultCamelContext(registry); context.addComponent("sjms-batch", sjmsBatchComponent); context.addComponent("sjms", sjmsComponent); return context; }
/** * This method establishes a connection to the ESB and also Instantiates a producer which can * deliver messages to the "ctms-caaers.inputQueue". */ public static void main(String[] args) { // Required fields ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; MessageProducer producer = null; StringBuilder contents = new StringBuilder(); BufferedReader input = null; File xmlFile = null; ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(); // Queue Instantiated with the Queue name from the ESB. ActiveMQQueue sendQueue = new ActiveMQQueue("ctms-caaers.inputQueue"); try { // Connection to the ESB. mqConnectionFactory.setBrokerURL("tcp://localhost:61616"); connectionFactory = mqConnectionFactory; connection = connectionFactory.createConnection(); // Obtaining a Session. session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // Instance of producer configured to deliver JMS Messages to the ""ctms-caaers.inputQueue" // Queue producer = session.createProducer(sendQueue); // For testing purpose the Study/Participant Xml is read from the file system. // In reality the Local Ctms System generates this Study/Participant Xml Message. xmlFile = getResources("classpath*:gov/nih/nci/cabig/caaers/impl/studydata/CreateStudyTest.xml")[0] .getFile(); // Reading the File Contents into Memory. input = new BufferedReader(new FileReader(xmlFile)); String line = null; while ((line = input.readLine()) != null) { contents.append(line); } // Create anew JMS TextMessage TextMessage message = session.createTextMessage(); // Here a sample ID is used. In reality the Local Ctms System has to have a // process to generate unique ID's. message.setJMSCorrelationID("103"); // Since this sample dealing with creating a Study in caaers the MESSAGE_TYPE is CREATE_STUDY. // If we need to update an existing Study in caaers then we have to set the MESSAGE_TYPE to // UPDATE_STUDY. message.setStringProperty("MESSAGE_TYPE", "CREATE_STUDY"); /* message.setStringProperty("MESSAGE_TYPE", "UPDATE_STUDY"); message.setStringProperty("MESSAGE_TYPE", "CREATE_PARTICIPANT"); message.setStringProperty("MESSAGE_TYPE", "UPDATE_PARTICIPANT"); message.setStringProperty("MESSAGE_TYPE", "CREATE_INVESTIGATOR"); message.setStringProperty("MESSAGE_TYPE", "UPDATE_INVESTIGATOR"); message.setStringProperty("MESSAGE_TYPE", "CREATE_RESEARCHSTAFF"); message.setStringProperty("MESSAGE_TYPE", "UPDATE_RESEARCHSTAFF"); */ // Setting the File Contents in memory as the PayLoad of the JMS TextMessage. message.setText(contents.toString()); // Call to deliver the JMS Message to the Queue. producer.send(message); // Close producer.close(); } catch (Exception e) { System.out.println(e.getMessage()); } finally { try { if (input != null) { input.close(); } if (connection != null) { connection.close(); } } catch (Exception e) { e.printStackTrace(); } } }
@Test public void testCloseWhenHunk() throws Exception { ActiveMQConnectionFactory connectionFactory1 = new ActiveMQConnectionFactory(); connectionFactory1.setBrokerURL( "vm://localhost?brokerConfig=xbean:embedded-activemq-config.xml"); connectionFactory1.setUseAsyncSend(true); connectionFactory1.setWatchTopicAdvisories(false); connectionFactory1.setOptimizeAcknowledge(true); connectionFactory1.setAlwaysSessionAsync(false); // start up the embedded broker which is running TCP on non-standard port connectionFactory1.createConnection().start(); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL("tcp://localhost:61666"); // TINY QUEUE is flow controlled after 1024 bytes final ActiveMQDestination destination = ActiveMQDestination.createDestination("queue://TINY_QUEUE", (byte) 0xff); Connection connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); final MessageProducer producer = session.createProducer(destination); producer.setTimeToLive(0); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); final AtomicReference<Exception> publishException = new AtomicReference<Exception>(null); final AtomicReference<Exception> closeException = new AtomicReference<Exception>(null); final AtomicLong lastLoop = new AtomicLong(System.currentTimeMillis() + 100); Thread pubThread = new Thread( new Runnable() { @Override public void run() { try { byte[] data = new byte[1000]; new Random(0xdeadbeef).nextBytes(data); for (int i = 0; i < 10000; i++) { lastLoop.set(System.currentTimeMillis()); ObjectMessage objMsg = session.createObjectMessage(); objMsg.setObject(data); producer.send(destination, objMsg); } } catch (Exception e) { publishException.set(e); } } }, "PublishingThread"); pubThread.start(); // wait for publisher to deadlock while (System.currentTimeMillis() - lastLoop.get() < 1000) { Thread.sleep(100); } System.out.println("Publisher deadlock detected."); Thread closeThread = new Thread( new Runnable() { @Override public void run() { try { System.out.println("Attempting close.."); producer.close(); } catch (Exception e) { closeException.set(e); } } }, "ClosingThread"); closeThread.start(); try { closeThread.join(10000); } catch (InterruptedException ie) { assertFalse("Closing thread didn't complete in 10 seconds", true); } try { pubThread.join(10000); } catch (InterruptedException ie) { assertFalse("Publishing thread didn't complete in 10 seconds", true); } assertNull(closeException.get()); assertNotNull(publishException.get()); }