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(); }
/** * 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 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 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 testWithTopicConnection() throws JMSException { MockControl conControl = MockControl.createControl(TopicConnection.class); Connection con = (TopicConnection) conControl.getMock(); con.start(); conControl.setVoidCallable(1); con.stop(); conControl.setVoidCallable(1); con.close(); conControl.setVoidCallable(1); conControl.replay(); SingleConnectionFactory scf = new SingleConnectionFactory(con); TopicConnection con1 = scf.createTopicConnection(); con1.start(); con1.stop(); // should be ignored con1.close(); // should be ignored TopicConnection con2 = scf.createTopicConnection(); con2.start(); con2.stop(); // should be ignored con2.close(); // should be ignored scf.destroy(); // should trigger actual close conControl.verify(); }
@Override public void setUp() throws Exception { super.setUp(); try { queueConnection = queueConnectionFactory.createQueueConnection(); queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); topicConnection = topicConnectionFactory.createTopicConnection(); topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); queueConnection.start(); topicConnection.start(); } catch (Exception e) { throw new RuntimeException(e); } }
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); } } } }
public void start() { try { conn.start(); } catch (JMSException e) { e.printStackTrace(); } }
public static TopicSubscriber getTopicSubscriber(TopicConnection cnn, String topicName) throws JMSException { TopicSession session = cnn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(topicName); TopicSubscriber receiver = session.createSubscriber(topic); return receiver; }
@Override public void stop() { logger.info("stopping " + toString()); stopped = true; try { connection.stop(); } catch (JMSException e) { logger.error("could not stop connection", e); } try { connection.close(); } catch (JMSException e) { logger.error("could not close connection", e); } super.stop(); }
@Override public void reconnect() { int numTries = 0; int pauseInMs = 100; boolean connected = false; if (connectionFactory == null) { logger.warn("Asked to reconnect to AMQ but no connectionFactory was configured!"); return; } synchronized (connectionMonitor) { close(); while (!connected) { numTries++; try { connection = connectionFactory.createTopicConnection(); connection.start(); connected = true; } catch (JMSException ex) { logger.warn("Got error while trying to connect to activemq"); try { Thread.sleep((long) pauseInMs); } catch (InterruptedException innerEx) { Thread.currentThread().interrupt(); return; } if (numTries < 10) { pauseInMs += pauseInMs; } } } } }
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 { } }
public void stop() { try { session.close(); } catch (JMSException e) { e.printStackTrace(); } try { conn.close(); } catch (JMSException e) { e.printStackTrace(); } }
TopicSession createTopicSession() { TopicSession result = null; while (result == null) { try { result = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } catch (JMSException ex) { reconnect(); } } return result; }
@Override public void close() { synchronized (connectionMonitor) { if (connection != null) { try { connection.close(); } catch (JMSException ex) { logger.error("Error while closing the connection to ActiveMQ", ex); } connection = null; } } }
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 == "); } }
@Override public void tearDown() throws Exception { try { queueConnection.close(); topicConnection.close(); } catch (Exception ignored) { } finally { queueConnection = null; queueSession = null; topicConnection = null; topicSession = null; super.tearDown(); } }
// ========================================================================= public static void closeTopicConnection(TopicConnection topicConnection) { try { if (topicConnection != null) { topicConnection.close(); } } catch (Exception e) { e.printStackTrace(); } finally { topicConnection = null; } } // end closeTopicConnection
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?"); } } }); }
public static void drainTopic(TopicConnection cnn, String topic) throws Exception { TopicSession session = cnn.createTopicSession(false, Session.DUPS_OK_ACKNOWLEDGE); Topic t = session.createTopic(topic); TopicSubscriber subscriber = session.createSubscriber(t); Message msg = subscriber.receiveNoWait(); while (msg != null) { try { msg.acknowledge(); } catch (JMSException e) { } msg = subscriber.receiveNoWait(); } subscriber.close(); session.close(); }
public static void topicPublish( TopicConnection cnn, String topicName, String payload, boolean transacted, int ack, String replyTo) throws JMSException { TopicSession session = cnn.createTopicSession(transacted, ack); Topic topic = session.createTopic(topicName); TopicPublisher publisher = session.createPublisher(topic); TextMessage msg = session.createTextMessage(); msg.setText(payload); msg.setJMSDeliveryMode(ack); if (replyTo != null) { msg.setJMSReplyTo(session.createTopic(replyTo)); } publisher.publish(msg); publisher.close(); session.close(); }
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); } }
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); } }
/** See TCK test: topicconntests.connNotStartedTopicTest */ public void testCannotReceiveMessageOnStoppedConnection() throws Exception { TopicConnection conn1 = ((TopicConnectionFactory) cf).createTopicConnection(); TopicConnection conn2 = ((TopicConnectionFactory) cf).createTopicConnection(); TopicSession sess1 = conn1.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicSession sess2 = conn2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicSubscriber sub1 = sess1.createSubscriber(topic1); TopicSubscriber sub2 = sess2.createSubscriber(topic1); conn1.start(); Connection conn3 = cf.createConnection(); Session sess3 = conn3.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer prod = sess3.createProducer(topic1); prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT); final int NUM_MESSAGES = 10; for (int i = 0; i < NUM_MESSAGES; i++) { TextMessage tm = sess3.createTextMessage("hello"); prod.send(tm); } int count = 0; while (true) { TextMessage tm = (TextMessage) sub1.receive(500); if (tm == null) { break; } assertEquals("hello", tm.getText()); count++; } assertEquals(NUM_MESSAGES, count); Message m = sub2.receive(200); assertNull(m); conn2.start(); count = 0; while (true) { TextMessage tm = (TextMessage) sub2.receive(500); if (tm == null) { break; } assertEquals("hello", tm.getText()); count++; } assertEquals(NUM_MESSAGES, count); log.debug("all messages received by sub2"); conn1.close(); conn2.close(); conn3.close(); }
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(); }