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; }
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(); }
/** 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 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); } }