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); } }
@Test public void testWithTopicConnectionFactoryAndJms11Usage() throws JMSException { TopicConnectionFactory cf = mock(TopicConnectionFactory.class); TopicConnection con = mock(TopicConnection.class); given(cf.createConnection()).willReturn(con); SingleConnectionFactory scf = new SingleConnectionFactory(cf); Connection con1 = scf.createConnection(); Connection con2 = scf.createConnection(); con1.start(); con2.start(); con1.close(); con2.close(); scf.destroy(); // should trigger actual close verify(con).start(); verify(con).stop(); verify(con).close(); verifyNoMoreInteractions(con); }
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+
ctx = new InitialContext(); } else { FileInputStream is = new FileInputStream(jndiPath); p.load(is); is.close(); ctx = new InitialContext(p); providerUrl = (String)ctx.getEnvironment().get(Context.PROVIDER_URL); TopicConnectionFactory topicConnectionFactory; topicConnectionFactory = topicConnectionFactory.createTopicConnection(userId, password); } else { topicConnection = topicConnectionFactory.createTopicConnection(); topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = (Topic)ctx.lookup(topicName); TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic); topicConnection.start();
.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);
protected void connect() { try { Context ctx = newContext(); TopicConnectionFactory tcf = (TopicConnectionFactory) ctx.lookup(_tcfName); Topic topic = (Topic) ctx.lookup(_topicName); ctx.close(); _connection = tcf.createTopicConnection(); // false == not transacted. _session = _connection.createTopicSession (false, Session.AUTO_ACKNOWLEDGE); // create a publisher _publisher = _session.createPublisher(topic); // create a subscriber. TopicSubscriber s = _session.createSubscriber(topic, null, /* noLocal: */ true); MessageListener l = getMessageListener(); s.setMessageListener(l); _connection.start(); _connection.setExceptionListener(this); if (log.isInfoEnabled()) log.info(s_loc.get("jms-start-listener", _topicName)); } catch (OpenJPAException ke) { throw ke; } catch (Exception e) { throw new UserException(s_loc.get("jms-provider-config", _topicName, _tcfName), e).setFatal(true); } }
ppt2.put(Context.PROVIDER_URL, "tcp://localhost:61616"); ppt2.put("topic.MessageDestinationTopic", "console.jms/TopicQueue/JCAAdminObject/MessageDestinationTopic"); Context ctx2 = new InitialContext(ppt2); TopicConnectionFactory factory = (TopicConnectionFactory) ctx2.lookup("ConnectionFactory"); TopicConnection connection = factory.createTopicConnection(); TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = (Topic) ctx2.lookup("MessageDestinationTopic"); MessageProducer producer = session.createProducer(topic); TextMessage msg = session.createTextMessage(); msg.setText("this is a test message"); producer.send(msg); producer.close(); session.close(); System.out.println("Message published. Please check application server's console to see the response from MDB"); ctx2.close(); System.exit(0);
public void lookupJMSConnectionFactory() throws TestFailureException{ try{ try{ 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); 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); 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); 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); } }
InitialContext ctx = new InitialContext(properties); TopicConnectionFactory connectionFactory = (TopicConnectionFactory) ctx .lookup(CONNECTION_FACTORY_NAME); TopicConnection connection = connectionFactory.createTopicConnection(); TopicSession session = connection.createTopicSession(false, 0); Topic topic = (Topic) ctx.lookup(TOPIC_NAME); TopicPublisher sender = session.createPublisher(topic);
InitialContext iniCtx = new InitialContext(); Object tmp = iniCtx.lookup("ConnectionFactory"); TopicConnectionFactory tcf = (TopicConnectionFactory) tmp; conn = tcf.createTopicConnection(); topic = (Topic) iniCtx.lookup("topic/testTopic"); session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE); conn.start();
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){
public TopicConnection getTopicConnection(String userName) throws EventBrokerException { InitialContext initialContext = null; try { initialContext = new InitialContext(getInitialContextProperties(userName, EventBrokerHolder.getInstance().getQpidServerDetails().getAccessKey())); TopicConnectionFactory topicConnectionFactory = getTopicConnectionFactory(initialContext); TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(); topicConnection.start(); return topicConnection; } catch (NamingException e) { throw new EventBrokerException("Can not create the initial context", e); } catch (JMSException e) { throw new EventBrokerException("Can not create topic connection", e); } catch (Exception e){ throw new EventBrokerException("Can not create topic connection", e); } finally { if (initialContext != null){ try { initialContext.close(); } catch (NamingException e) { log.error("Can not close the inital context factory ", e); } } } }
public void lookupJMSConnectionFactory() throws TestFailureException{ try{ try{ testJmsConnection(coonnectionFactory.createConnection()); testJmsConnection(queueCoonnectionFactory.createConnection()); testJmsConnection(topicCoonnectionFactory.createConnection()); } catch (Exception e){ e.printStackTrace(); Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage()); } } catch (AssertionFailedError afe){ throw new TestFailureException(afe); } }
Object tmp = iniCtx.lookup(this.connectionFactoryName); TopicConnectionFactory tcf = (TopicConnectionFactory) tmp; conn = tcf.createTopicConnection(); topic = (Topic) iniCtx.lookup(this.topicName); session = conn.createTopicSession(this.transacted, this.acknowledgeMode); conn.start(); this.propagationMode == JMSCacheInvalidationBridgeMBean.IN_ONLY_BRIDGE_PROPAGATION) this.subscriber = session.createSubscriber(topic); this.subscriber.setMessageListener(this); this.propagationMode == JMSCacheInvalidationBridgeMBean.OUT_ONLY_BRIDGE_PROPAGATION) this.pub = session.createPublisher(topic); this.publishingAuthorized = true;
publisherTCF = (TopicConnectionFactory) ctx.lookup(PubSubTestCase.TCF_NAME); publisherTopic = (Topic) ctx.lookup(PubSubTestCase.TOPIC_NAME); publisherConnection = publisherTCF.createTopicConnection(); publisherConnection.setClientID("publisherConnection"); publisherSession = publisherConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); publisher = publisherSession.createPublisher(publisherTopic); subscriberTCF = (TopicConnectionFactory) ctx.lookup(PubSubTestCase.TCF_NAME); subscriberTopic = (Topic) ctx.lookup(PubSubTestCase.TOPIC_NAME); subscriberConnection = subscriberTCF.createTopicConnection(); subscriberConnection.setClientID("subscriberConnection"); subscriberSession = subscriberConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); subscriber = subscriberSession.createSubscriber(subscriberTopic); publisherConnection.start();
public synchronized void load() throws GenericServiceException { try { InitialContext jndi = JNDIContextFactory.getInitialContext(jndiServer); TopicConnectionFactory factory = (TopicConnectionFactory) jndi.lookup(jndiName); con = factory.createTopicConnection(userName, password); con.setExceptionListener(this); session = con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); topic = (Topic) jndi.lookup(topicName); if (topic != null) { TopicSubscriber subscriber = session.createSubscriber(topic); subscriber.setMessageListener(this); con.start(); this.setConnected(true); if (Debug.infoOn()) Debug.logInfo("Listening to topic [" + topicName + "] on [" + jndiServer + "]...", module);
/** * Test that ConnectionFactory can be cast to TopicConnectionFactory and TopicConnection can be * created. */ @Test public void testTopicConnectionFactory() throws Exception { deployConnectionFactory(0, JMSFactoryType.TOPIC_CF, "CF_TOPIC_XA_FALSE", "/CF_TOPIC_XA_FALSE"); TopicConnectionFactory qcf = (TopicConnectionFactory) ic.lookup("/CF_TOPIC_XA_FALSE"); TopicConnection tc = qcf.createTopicConnection(); tc.close(); undeployConnectionFactory("CF_TOPIC_XA_FALSE"); }
/** * Create a JMS Connection via this template's ConnectionFactory. * @return the new JMS Connection * @throws javax.jms.JMSException if thrown by JMS API methods */ protected Connection doCreateConnection() throws JMSException { ConnectionFactory cf = getTargetConnectionFactory(); if (Boolean.FALSE.equals(this.pubSubMode) && cf instanceof QueueConnectionFactory) { return ((QueueConnectionFactory) cf).createQueueConnection(); } else if (Boolean.TRUE.equals(this.pubSubMode) && cf instanceof TopicConnectionFactory) { return ((TopicConnectionFactory) cf).createTopicConnection(); } else { return obtainTargetConnectionFactory().createConnection(); } }
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();
@Override public TopicConnection createTopicConnection(String username, String password) throws JMSException { ConnectionFactory target = obtainTargetConnectionFactory(); if (target instanceof TopicConnectionFactory) { return ((TopicConnectionFactory) target).createTopicConnection(username, password); } else { Connection con = target.createConnection(username, password); if (!(con instanceof TopicConnection)) { throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a TopicConnectionFactory"); } return (TopicConnection) con; } }