public JMSSink( String tcfBindingName, String topicBindingName, String username, String password) { try { Context ctx = new InitialContext(); TopicConnectionFactory topicConnectionFactory; topicConnectionFactory = (TopicConnectionFactory) lookup(ctx, tcfBindingName); TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(username, password); topicConnection.start(); TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = (Topic)ctx.lookup(topicBindingName); TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic); topicSubscriber.setMessageListener(this); } catch(JMSException e) { logger.error("Could not read JMS message.", e); } catch(NamingException e) { logger.error("Could not read JMS message.", e); } catch(RuntimeException e) { logger.error("Could not read JMS message.", e); } }
jndi = new InitialContext(env); } else { jndi = new InitialContext(); LogLog.debug("About to create TopicConnection."); if(userName != null) { topicConnection = topicConnectionFactory.createTopicConnection(userName, password); } else { topicConnection = topicConnectionFactory.createTopicConnection(); topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); topicPublisher = topicSession.createPublisher(topic); topicConnection.start(); jndi.close(); } catch(JMSException e) { errorHandler.error("Error while activating options for appender named ["+name+
/** Close this JMSAppender. Closing releases all resources used by the appender. A closed appender cannot be re-opened. */ public synchronized void close() { // The synchronized modifier avoids concurrent append and close operations if(this.closed) return; LogLog.debug("Closing appender ["+name+"]."); this.closed = true; try { if(topicSession != null) topicSession.close(); if(topicConnection != null) topicConnection.close(); } catch(JMSException e) { LogLog.error("Error while closing JMSAppender ["+name+"].", e); } catch(RuntimeException e) { LogLog.error("Error while closing JMSAppender ["+name+"].", e); } // Help garbage collection topicPublisher = null; topicSession = null; topicConnection = null; }
/** Constructor for the {@code TopicRequestor} class. * * <P>This implementation assumes the session parameter to be non-transacted, * with a delivery mode of either {@code AUTO_ACKNOWLEDGE} or * {@code DUPS_OK_ACKNOWLEDGE}. * * @param session the {@code TopicSession} the topic belongs to * @param topic the topic to perform the request/reply call on * * @exception JMSException if the JMS provider fails to create the * {@code TopicRequestor} due to some internal * error. * @exception InvalidDestinationException if an invalid topic is specified. */ public TopicRequestor(TopicSession session, Topic topic) throws JMSException { if (topic==null) throw new InvalidDestinationException("topic==null"); this.session = session; tempTopic = session.createTemporaryTopic(); publisher = session.createPublisher(topic); subscriber = session.createSubscriber(tempTopic); }
@Override protected MessageConsumer createConsumer() throws JMSException { // set up the consumer if (consumerConnection == null) return null; consumerSession = consumerConnection.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE); MessageConsumer consumer = null; if (consumerName != null && consumerName.length() > 0) { if (selector != null && selector.length() > 0) { consumer = consumerSession.createDurableSubscriber(consumerTopic, consumerName, selector, false); } else { consumer = consumerSession.createDurableSubscriber(consumerTopic, consumerName); } } else { if (selector != null && selector.length() > 0) { consumer = consumerSession.createSubscriber(consumerTopic, selector, false); } else { consumer = consumerSession.createSubscriber(consumerTopic); } } consumer.setMessageListener(this); return consumer; }
public JMSSink( String tcfBindingName, String topicBindingName, String username, String password) { try { Context ctx = new InitialContext(); TopicConnectionFactory topicConnectionFactory; topicConnectionFactory = (TopicConnectionFactory) lookup(ctx, tcfBindingName); TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(username, password); topicConnection.start(); TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = (Topic)ctx.lookup(topicBindingName); TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic); topicSubscriber.setMessageListener(this); } catch(Exception e) { logger.error("Could not read JMS message.", e); } }
Context ctx = new InitialContext(); // lookup the topic connection factory TopicConnectionFactory connFactory = (TopicConnectionFactory) ctx .lookup("/RemoteConnectionFactory"); // lookup the topic object Topic topic = (Topic) ctx.lookup("java:/"+prop.TOPIC); // create a topic connection TopicConnection topicConn = connFactory.createTopicConnection(prop.USERPUB,prop.PASSPUB); //create topic session TopicSession topicSession = topicConn.createTopicSession(true, Session.AUTO_ACKNOWLEDGE); //create topic publisher MessageProducer topicPublisher = topicSession.createPublisher(topic); topicPublisher.setDeliveryMode(DeliveryMode.PERSISTENT);
public void run(String dest) throws Exception { InitialContext ic = getInitialContext(); TopicConnectionFactory confac = (TopicConnectionFactory) ic.lookup("ConnectionFactory"); TopicConnection connection = confac.createTopicConnection(); TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(dest); TopicSubscriber topicSubscriber = session.createSubscriber(topic); topicSubscriber.setMessageListener(this); System.out.println("MDD-Consumer listening for topic : "+topic.getTopicName()); connection.start(); } public void onMessage(Message message){
.lookup(dataMap .getString(JmsHelper.JMS_CONNECTION_FACTORY_JNDI)); conn = connFactory.createTopicConnection(); } else { final String user = dataMap.getString(JmsHelper.JMS_USER); .getString(JmsHelper.JMS_PASSWORD); conn = connFactory.createTopicConnection(user, password); sess = conn.createTopicSession(useTransaction, ackMode); final Topic topic = (Topic) namingCtx.lookup(dataMap .getString(JmsHelper.JMS_DESTINATION_JNDI)); publisher = sess.createPublisher(topic); publisher.publish(msg); } catch (final Exception e) { throw new JobExecutionException(e);
/** * Test that a durable subscriber effectively receives the messages sent to * its topic while it was inactive. */ public void testDurableSubscriber() { try { subscriber = subscriberSession.createDurableSubscriber(subscriberTopic, "testTopic"); subscriberConnection.close(); subscriberConnection = null; TextMessage message = publisherSession.createTextMessage(); message.setText("test"); publisher.publish(message); subscriberConnection = subscriberTCF.createTopicConnection(); subscriberConnection.setClientID("subscriberConnection"); subscriberSession = subscriberConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); subscriber = subscriberSession.createDurableSubscriber(subscriberTopic, "testTopic"); subscriberConnection.start(); TextMessage m = (TextMessage) subscriber.receive(TestConfig.TIMEOUT); Assert.assertTrue(m != null); Assert.assertEquals("test", m.getText()); } catch (JMSException e) { fail(e); } }
publisherConnection.stop(); subscriberConnection.stop(); tempTopic = subscriberSession.createTemporaryTopic(); publisher = publisherSession.createPublisher(tempTopic); tempSubscriber = subscriberSession.createSubscriber(tempTopic); subscriberConnection.start(); publisherConnection.start(); TextMessage message = publisherSession.createTextMessage(); message.setText("testTemporaryTopic"); publisher.publish(message); Message m = tempSubscriber.receive(TestConfig.TIMEOUT); assertTrue(m instanceof TextMessage); TextMessage msg = (TextMessage) m;
/** * Topics shouldn't hold on to messages when the non-durable subscribers close */ @Test public void testPersistentMessagesForTopicDropped2() throws Exception { TopicConnection topicConn = createTopicConnection(); topicConn.start(); TopicSession sess = topicConn.createTopicSession(true, 0); TopicPublisher pub = sess.createPublisher(ActiveMQServerTestCase.topic1); TopicSubscriber sub = sess.createSubscriber(ActiveMQServerTestCase.topic1); pub.setDeliveryMode(DeliveryMode.PERSISTENT); Message m = sess.createTextMessage("testing123"); pub.publish(m); sess.commit(); // receive but rollback TextMessage m2 = (TextMessage) sub.receive(3000); ProxyAssertSupport.assertNotNull(m2); ProxyAssertSupport.assertEquals("testing123", m2.getText()); sess.rollback(); topicConn.close(); checkEmpty(ActiveMQServerTestCase.topic1); }
TopicSession nonTxSession = mock(TopicSession.class); given(cf.createTopicConnection()).willReturn(con); given(con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession); given(txSession.getTransacted()).willReturn(true); given(con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession); con1.start(); TopicConnection con2 = scf.createTopicConnection(); Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE); session2.close(); session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE); verify(txSession).close(); verify(nonTxSession).close(); verify(con).start(); verify(con).stop();
/** * Topics shouldn't hold on to messages if there are no subscribers */ @Test public void testPersistentMessagesForTopicDropped() throws Exception { TopicConnection topicConn = createTopicConnection(); TopicSession sess = topicConn.createTopicSession(true, 0); TopicPublisher pub = sess.createPublisher(ActiveMQServerTestCase.topic1); pub.setDeliveryMode(DeliveryMode.PERSISTENT); Message m = sess.createTextMessage("testing123"); pub.publish(m); sess.commit(); topicConn.close(); checkEmpty(ActiveMQServerTestCase.topic1); }
public void subscribe(TopicConnection conn) throws JMSException { topicSession = conn.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); topic = topicSession.createTopic(topicName); subscriber = topicSession.createSubscriber(topic); }
@Test public void testNotificationProperties() throws Exception { try (TopicConnection topicConnection = factory.createTopicConnection()) { TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic notificationsTopic = topicSession.createTopic("activemq.notifications"); TopicSubscriber subscriber = topicSession.createSubscriber(notificationsTopic); List<Message> receivedMessages = new CopyOnWriteArrayList<>(); subscriber.setMessageListener(receivedMessages::add); topicConnection.start(); Wait.waitFor(() -> receivedMessages.size() > 0); Assert.assertTrue(receivedMessages.size() > 0); for (Message message : receivedMessages) { assertNotNull(message); assertNotNull(message.getStringProperty("_AMQ_NotifType")); } } }
protected void initializeInboundDestinationBridgesOutboundSide(TopicConnection connection) throws JMSException { if (inboundTopicBridges != null) { TopicSession outboundSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); for (InboundTopicBridge bridge : inboundTopicBridges) { String TopicName = bridge.getInboundTopicName(); Topic foreignTopic = createForeignTopic(outboundSession, TopicName); bridge.setConsumer(null); bridge.setConsumerTopic(foreignTopic); bridge.setConsumerConnection(connection); bridge.setJmsConnector(this); addInboundBridge(bridge); } outboundSession.close(); } }
/** This method called by {@link AppenderSkeleton#doAppend} method to do most of the real appending work. */ public void append(LoggingEvent event) { if(!checkEntryConditions()) { return; } try { ObjectMessage msg = topicSession.createObjectMessage(); if(locationInfo) { event.getLocationInformation(); } msg.setObject(event); topicPublisher.publish(msg); } catch(JMSException e) { errorHandler.error("Could not publish message in JMSAppender ["+name+"].", e, ErrorCode.GENERIC_FAILURE); } catch(RuntimeException e) { errorHandler.error("Could not publish message in JMSAppender ["+name+"].", e, ErrorCode.GENERIC_FAILURE); } }
@Override protected synchronized MessageProducer createProducer() throws JMSException { if (producerConnection == null) return null; producerSession = producerConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); producer = producerSession.createPublisher(null); return producer; }