public void lookupJMSConnectionFactory() throws TestFailureException { try { try { final InitialContext ctx = new InitialContext(); Assert.assertNotNull("The InitialContext is null", ctx); Object obj = ctx.lookup("java:comp/env/jms"); Assert.assertNotNull("The JMS ConnectionFactory is null", obj); Assert.assertTrue("Not an instance of ConnectionFactory", obj instanceof ConnectionFactory); final ConnectionFactory connectionFactory = (ConnectionFactory) obj; testJmsConnection(connectionFactory.createConnection()); obj = ctx.lookup("java:comp/env/TopicCF"); Assert.assertNotNull("The JMS TopicConnectionFactory is null", obj); Assert.assertTrue( "Not an instance of TopicConnectionFactory", obj instanceof TopicConnectionFactory); final TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj; testJmsConnection(topicConnectionFactory.createConnection()); obj = ctx.lookup("java:comp/env/QueueCF"); Assert.assertNotNull("The JMS QueueConnectionFactory is null", obj); Assert.assertTrue( "Not an instance of QueueConnectionFactory", obj instanceof QueueConnectionFactory); final QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj; testJmsConnection(queueConnectionFactory.createConnection()); } catch (final Exception e) { e.printStackTrace(); Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage()); } } catch (final AssertionFailedError afe) { throw new TestFailureException(afe); } }
public void testConnectionFactory102WithTopic() throws JMSException { MockControl cfControl = MockControl.createControl(TopicConnectionFactory.class); TopicConnectionFactory cf = (TopicConnectionFactory) cfControl.getMock(); MockControl conControl = MockControl.createControl(TopicConnection.class); TopicConnection con = (TopicConnection) conControl.getMock(); cf.createTopicConnection(); cfControl.setReturnValue(con, 1); con.start(); conControl.setVoidCallable(1); con.stop(); conControl.setVoidCallable(1); con.close(); conControl.setVoidCallable(1); cfControl.replay(); conControl.replay(); SingleConnectionFactory scf = new SingleConnectionFactory102(cf, true); TopicConnection con1 = scf.createTopicConnection(); con1.start(); con1.close(); // should be ignored TopicConnection con2 = scf.createTopicConnection(); con2.start(); con2.close(); // should be ignored scf.destroy(); // should trigger actual close cfControl.verify(); conControl.verify(); }
public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage() throws JMSException { MockControl cfControl = MockControl.createControl(TopicConnectionFactory.class); TopicConnectionFactory cf = (TopicConnectionFactory) cfControl.getMock(); MockControl conControl = MockControl.createControl(TopicConnection.class); TopicConnection con = (TopicConnection) conControl.getMock(); MockControl txSessionControl = MockControl.createControl(TopicSession.class); TopicSession txSession = (TopicSession) txSessionControl.getMock(); MockControl nonTxSessionControl = MockControl.createControl(TopicSession.class); TopicSession nonTxSession = (TopicSession) nonTxSessionControl.getMock(); cf.createTopicConnection(); cfControl.setReturnValue(con, 1); con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE); conControl.setReturnValue(txSession, 1); txSession.getTransacted(); txSessionControl.setReturnValue(true, 2); txSession.close(); txSessionControl.setVoidCallable(1); con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE); conControl.setReturnValue(nonTxSession, 1); nonTxSession.close(); nonTxSessionControl.setVoidCallable(1); con.start(); conControl.setVoidCallable(1); con.stop(); conControl.setVoidCallable(1); con.close(); conControl.setVoidCallable(1); cfControl.replay(); conControl.replay(); txSessionControl.replay(); nonTxSessionControl.replay(); CachingConnectionFactory scf = new CachingConnectionFactory(cf); scf.setReconnectOnException(false); Connection con1 = scf.createTopicConnection(); Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE); session1.getTransacted(); session1.close(); // should lead to rollback session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE); session1.close(); // should be ignored con1.start(); con1.close(); // should be ignored TopicConnection con2 = scf.createTopicConnection(); Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE); session2.close(); // should be ignored session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); session2.getTransacted(); session2.close(); // should be ignored con2.start(); con2.close(); // should be ignored scf.destroy(); // should trigger actual close cfControl.verify(); conControl.verify(); txSessionControl.verify(); nonTxSessionControl.verify(); }
public static TopicConnection getTopicConnection(Properties props) throws IOException, NamingException, JMSException { fixProviderUrl(props); String cnnFactoryName = props.getProperty(JNDI_TOPIC_CONECTION_NAME_PROPERTY); if (cnnFactoryName == null) { throw new IOException( "You must set the property " + DEFAULT_JNDI_CONECTION_NAME_PROPERTY + "in the JNDI property file"); } Context ctx = new InitialContext(props); TopicConnectionFactory tcf = (TopicConnectionFactory) lookupObject(ctx, cnnFactoryName); TopicConnection cnn; String username = (String) props.get("username"); if (username != null) { String password = (String) props.get("password"); cnn = tcf.createTopicConnection(username, password); } else { cnn = tcf.createTopicConnection(); } cnn.start(); return cnn; }
public void lookupJMSConnectionFactory() throws TestFailureException { try { try { Object obj = ejbContext.lookup("jms"); Assert.assertNotNull("The JMS ConnectionFactory is null", obj); Assert.assertTrue("Not an instance of ConnectionFactory", obj instanceof ConnectionFactory); ConnectionFactory connectionFactory = (ConnectionFactory) obj; testJmsConnection(connectionFactory.createConnection()); obj = ejbContext.lookup("TopicCF"); Assert.assertNotNull("The JMS TopicConnectionFactory is null", obj); Assert.assertTrue( "Not an instance of TopicConnectionFactory", obj instanceof TopicConnectionFactory); TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj; testJmsConnection(topicConnectionFactory.createConnection()); obj = ejbContext.lookup("QueueCF"); Assert.assertNotNull("The JMS QueueConnectionFactory is null", obj); Assert.assertTrue( "Not an instance of QueueConnectionFactory", obj instanceof QueueConnectionFactory); QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj; testJmsConnection(queueConnectionFactory.createConnection()); } catch (Exception e) { e.printStackTrace(); Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage()); } } catch (AssertionFailedError afe) { throw new TestFailureException(afe); } }
public ChatPublisher(String name, String topicName, boolean isDurable) throws JMSException { this.name = name; TopicConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616"); conn = factory.createTopicConnection(); session = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); pub = session.createPublisher(new ActiveMQTopic(topicName)); if (isDurable) { pub.setDeliveryMode(DeliveryMode.PERSISTENT); // pub.setTimeToLive(20000); System.out.println(" === persistence == "); } }
protected void produceJMSMessage(SerializableEventBundle message) throws JMSBusNotActiveException { InitialContext ctx; Topic nuxeoTopic; try { ctx = new InitialContext(); nuxeoTopic = (Topic) ctx.lookup(NUXEO_JMS_TOPIC); } catch (NamingException e) { jmsBusIsActive = false; throw new JMSBusNotActiveException(e); } TopicConnection nuxeoTopicConnection = null; TopicSession nuxeoTopicSession = null; TopicPublisher nuxeoMessagePublisher = null; try { TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("TopicConnectionFactory"); nuxeoTopicConnection = factory.createTopicConnection(); nuxeoTopicSession = nuxeoTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE); ObjectMessage jmsMessage = nuxeoTopicSession.createObjectMessage(message); // add Headers for JMS message jmsMessage.setStringProperty("BundleEvent", message.getEventBundleName()); nuxeoMessagePublisher = nuxeoTopicSession.createPublisher(nuxeoTopic); nuxeoMessagePublisher.send(jmsMessage); log.debug("Event bundle " + message.getEventBundleName() + " forwarded to JMS topic"); } catch (Exception e) { log.error("Error during JMS forwarding", e); } finally { if (nuxeoTopicSession != null) { try { if (nuxeoMessagePublisher != null) { nuxeoMessagePublisher.close(); } nuxeoTopicConnection.close(); nuxeoTopicSession.close(); } catch (JMSException e) { log.error("Error during JMS cleanup", e); } } } }
/** * Tries to reconnect to the durable topic. This method blocks until the try is successful or this * provider is stopped. */ private void reconnect() { for (; ; ) { if (stopped) { return; } try { // close subscriber if not previously closed if (subscriber != null) { subscriber.close(); } if (connection != null) { connection.close(); } connection = connectionFactory.createTopicConnection(); if (clientID != null) { connection.setClientID(clientID); } TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = topicFactory.createTopic(topicName); subscriber = session.createDurableSubscriber(topic, name); connection.start(); return; } catch (JMSException e) { logger.error("could not connect to durable topic, topic: " + topicName, e); // step back for a while backOffAfterJMSException(e); } } }
public void onMessage(Message message) { try { MapMessage request = (MapMessage) message; String correlationID = request.getJMSCorrelationID(); int itemId = request.getInt("itemId"); // Retrieve the connection factory connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); // get the bids history String html = getBidHistory(itemId); // send the reply TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); if (temporaryTopic != null) { // create a connection connection = connectionFactory.createTopicConnection(); // create a session: no transaction, auto ack session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TextMessage reply = session.createTextMessage(); reply.setJMSCorrelationID(correlationID); reply.setText(html); replier = session.createPublisher(null); // unidentified publisher connection.start(); replier.publish(temporaryTopic, reply); replier.close(); session.close(); connection.stop(); connection.close(); } } catch (Exception e) { throw new EJBException("Message traitment failed for MDB_ViewBidHistory: " + e); } }
public void start() throws JMSException, NamingException { Context ctx = getInitialContext(); TopicConnectionFactory tcf = (TopicConnectionFactory) ctx.lookup("ConnectionFactory"); Topic t = (Topic) ctx.lookup("topic/i_jms"); TopicConnection tconn = tcf.createTopicConnection(); TopicSession tses = tconn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicPublisher tpub = tses.createPublisher(t); tconn.start(); for (int i = 0; i < 10; i++) { Producer.sendMessage("Message= " + i, tses, tpub); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } tpub.close(); }
public void testTopicConnectionFactory() throws Exception { TopicConnectionFactory cf = null; TopicConnection c = null; try { cf = new QpidConnectionFactoryProxy(); ((QpidConnectionFactoryProxy) cf).setConnectionURL(URL); c = cf.createTopicConnection(); assertTrue(c instanceof TopicConnection); } finally { if (c != null) { c.close(); } } try { } finally { } }
private JMSProducer(final long timeout, String brokerURL) throws JMSException { // if brokerURL not defined before, get property if (brokerURL == null) { brokerURL = System.getProperty("broker.url"); } // Create a ConnectionFactory topicConnectionFactory = new ActiveMQConnectionFactory(brokerURL); topicConnection = topicConnectionFactory.createTopicConnection(); session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); topic = session.createTopic(TOPIC_NAME); topicConnection.start(); producer = session.createProducer(topic); this.timeout = timeout; Runtime.getRuntime() .addShutdownHook( new Thread() { @Override public void run() { try { topicConnection.stop(); producer.close(); session.close(); topicConnection.close(); } catch (final JMSException e) { System.err.println("Cannot stop the synchro service, probably already stopped?"); } } }); }
/** * Create a one time MessageProducer for this JMS OutTransport information. For simplicity and * best compatibility, this method uses only JMS 1.0.2b API. Please be cautious when making any * changes * * @return a JMSSender based on one-time use resources * @throws JMSException on errors, to be handled and logged by the caller */ public JMSMessageSender createJMSSender(MessageContext msgCtx) throws JMSException { // digest the targetAddress and locate CF from the EPR loadConnectionFactoryFromProperties(); // create a one time connection and session to be used String user = properties != null ? properties.get(JMSConstants.PARAM_JMS_USERNAME) : null; String pass = properties != null ? properties.get(JMSConstants.PARAM_JMS_PASSWORD) : null; QueueConnectionFactory qConFac = null; TopicConnectionFactory tConFac = null; int destType = -1; // TODO: there is something missing here for destination type generic if (JMSConstants.DESTINATION_TYPE_QUEUE.equals(destinationType)) { destType = JMSConstants.QUEUE; qConFac = (QueueConnectionFactory) connectionFactory; } else if (JMSConstants.DESTINATION_TYPE_TOPIC.equals(destinationType)) { destType = JMSConstants.TOPIC; tConFac = (TopicConnectionFactory) connectionFactory; } else { // treat jmsdestination type=queue(default is queue) destType = JMSConstants.QUEUE; qConFac = (QueueConnectionFactory) connectionFactory; } if (msgCtx.getProperty(JMSConstants.JMS_XA_TRANSACTION_MANAGER) != null) { XAConnection connection = null; if (user != null && pass != null) { if (qConFac != null) { connection = ((XAConnectionFactory) qConFac).createXAConnection(user, pass); } else if (tConFac != null) { connection = ((XAConnectionFactory) tConFac).createXAConnection(user, pass); } } else { if (qConFac != null) { connection = ((XAConnectionFactory) qConFac).createXAConnection(); } else if (tConFac != null) { connection = ((XAConnectionFactory) tConFac).createXAConnection(); } } if (connection == null) { connection = ((XAConnectionFactory) qConFac).createXAConnection(); } XASession session = null; MessageProducer producer = null; if (connection != null) { if (destType == JMSConstants.QUEUE) { session = connection.createXASession(); producer = session.createProducer(destination); } else { session = connection.createXASession(); producer = session.createProducer(destination); } } XAResource xaResource = session.getXAResource(); TransactionManager tx = null; Xid xid1 = null; Transaction transaction = null; java.util.UUID uuid = java.util.UUID.randomUUID(); try { tx = (TransactionManager) msgCtx.getProperty(JMSConstants.JMS_XA_TRANSACTION_MANAGER); transaction = tx.getTransaction(); msgCtx.setProperty(JMSConstants.JMS_XA_TRANSACTION_MANAGER, tx); msgCtx.setProperty(JMSConstants.JMS_XA_TRANSACTION, transaction); xid1 = new JMSXid( JMSConstants.JMS_XA_TRANSACTION_PREFIX.getBytes(StandardCharsets.UTF_8), 1, uuid.toString().getBytes()); msgCtx.setProperty("XID", xid1); xaResource.start(xid1, XAResource.TMNOFLAGS); } catch (SystemException e) { handleException("Error Occurred during starting getting Transaction.", e); } catch (XAException e) { handleException("Error Occurred during starting XA resource.", e); } return new JMSMessageSender( connection, session, producer, destination, jmsConnectionFactory == null ? this.cacheLevel : jmsConnectionFactory.getCacheLevel(), jmsSpecVersion, destType == -1 ? null : destType == JMSConstants.QUEUE ? Boolean.TRUE : Boolean.FALSE, transaction, xid1, xaResource); } else { Connection connection = null; 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 (connection == null) { connection = jmsConnectionFactory != null ? jmsConnectionFactory.getConnection() : null; } Session session = null; MessageProducer producer = null; if (connection != null) { if (destType == JMSConstants.QUEUE) { session = ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE); producer = ((QueueSession) session).createSender((Queue) destination); } else { session = ((TopicConnection) connection).createTopicSession(false, Session.AUTO_ACKNOWLEDGE); producer = ((TopicSession) session).createPublisher((Topic) destination); } } return new JMSMessageSender( connection, session, producer, destination, jmsConnectionFactory == null ? this.cacheLevel : jmsConnectionFactory.getCacheLevel(), jmsSpecVersion, destType == -1 ? null : destType == JMSConstants.QUEUE ? Boolean.TRUE : Boolean.FALSE); } }
public tibjmsLoadBalancedTopicPublisher(String[] args) { parseArgs(args); /* print parameters */ System.out.println( "\n------------------------------------------------------------------------"); System.out.println("tibjmsLoadBalancedTopicPublisher SAMPLE"); System.out.println("------------------------------------------------------------------------"); System.out.println("Servers...................... " + serverList); System.out.println("User......................... " + (userName != null ? userName : "******")); System.out.println("Topic........................ " + topicName); System.out.println("Messages..................... " + messages); System.out.println("Delay........................ " + delay); System.out.println( "------------------------------------------------------------------------\n"); System.err.println("Publishing on topic '" + topicName + "'\n"); try { HashMap properties = new HashMap(); Integer metric; if (balanceByConnections) metric = new Integer(Tibjms.FACTORY_LOAD_BALANCE_METRIC_CONNECTIONS); else metric = new Integer(Tibjms.FACTORY_LOAD_BALANCE_METRIC_BYTE_RATE); properties.put(Tibjms.FACTORY_LOAD_BALANCE_METRIC, metric); TopicConnectionFactory factory = new com.tibco.tibjms.TibjmsTopicConnectionFactory(serverList, null, properties); TopicConnection connection = factory.createTopicConnection(userName, password); TopicSession session = connection.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); /* * Use createTopic() to enable publishing into dynamic topics. */ javax.jms.Topic topic = session.createTopic(topicName); TopicPublisher publisher = session.createPublisher(topic); /* publish messages */ for (int i = 0; i < messages; i++) { javax.jms.TextMessage message = session.createTextMessage(); String text = "Load balanced message " + i; message.setText(text); publisher.publish(message); System.err.println( "Published message " + i + " to server " + Tibjms.getConnectionActiveURL(connection)); try { Thread.sleep(delay * 1000); } catch (InterruptedException e) { } } connection.close(); } catch (JMSException e) { e.printStackTrace(); System.exit(0); } }
public tibjmsTopicSubscriber(String[] args) { parseArgs(args); /* print parameters */ System.err.println( "\n------------------------------------------------------------------------"); System.err.println("tibjmsTopicSubscriber SAMPLE"); System.err.println("------------------------------------------------------------------------"); System.err.println( "Server....................... " + ((serverUrl != null) ? serverUrl : "localhost")); System.err.println( "User......................... " + ((userName != null) ? userName : "******")); System.err.println("Topic........................ " + topicName); System.err.println( "------------------------------------------------------------------------\n"); try { tibjmsUtilities.initSSLParams(serverUrl, args); } catch (JMSSecurityException e) { System.err.println( "JMSSecurityException: " + e.getMessage() + ", provider=" + e.getErrorCode()); e.printStackTrace(); System.exit(0); } if (topicName == null) { System.err.println("Error: must specify topic name"); usage(); } System.err.println("Subscribing to topic: " + topicName); try { TopicConnectionFactory factory = new com.tibco.tibjms.TibjmsTopicConnectionFactory(serverUrl); TopicConnection connection = factory.createTopicConnection(userName, password); TopicSession session = connection.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); /* * Use createTopic() to enable subscriptions to dynamic topics. */ javax.jms.Topic topic = session.createTopic(topicName); TopicSubscriber subscriber = session.createSubscriber(topic); connection.start(); /* read topic messages */ while (true) { javax.jms.Message message = subscriber.receive(); if (message == null) break; System.err.println("Received message: " + message); } connection.close(); } catch (JMSException e) { System.err.println("JMSException: " + e.getMessage() + ", provider=" + e.getErrorCode()); e.printStackTrace(); System.exit(0); } }