Package javax.jms

Examples of javax.jms.TopicConnection


        // set the default acknowledge mode to dups
        // concrete implementations may want to override this
        m_acknowledgeMode = Session.DUPS_OK_ACKNOWLEDGE;

        // register this MessageListener with a TopicSubscriber
        final TopicConnection connection = (TopicConnection) m_jmsConnectionManager.getConnection(m_connectionName);
        if (connection != null) {
            m_session = connection.createTopicSession(false, m_acknowledgeMode);
            final Topic topic = m_session.createTopic(m_topicName);
            if (m_subscriptionId != null) {
                m_subscriber = m_session.createDurableSubscriber(topic, m_subscriptionId, m_selector, false);
            }
            else {
View Full Code Here


        Context ctx = new InitialContext(namingProps);
        /* Lookup 'administerable' Objects */
        TopicConnectionFactory tcf = (TopicConnectionFactory)ctx.lookup("TopicConnectionFactory");
        Topic                  t   = (Topic)ctx.lookup("testTopic");

        TopicConnection tc  = null;
        TopicSession s_send = null;
        TopicSession s_rec1 = null;
        TopicSession s_rec2 = null;
        try {
            /* Create a connection to the topic */
            tc = tcf.createTopicConnection("system", "system");
            /* Create a session on top of the connection which will be used only for
            sending messages, transacted and with auto-acknowledge-mode*/
            s_send = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
            /* Create sessions on top of the connection which will be used only for
            receiving messages, transacted and with auto-acknowledge-mode */
            s_rec1 = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
            s_rec2 = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
            /* start the connection */
            tc.start();
            /* Create the receivers */
            TopicSubscriber ts1 = s_rec1.createSubscriber(t, "size >= 5 OR (size < 5 AND color ='green')", false);
            TopicSubscriber ts2 = s_rec1.createSubscriber(t);
            TopicSubscriber ts3 = s_rec2.createSubscriber(t);
            /* Create a sender for sending messages */
            TopicPublisher tpub = s_send.createPublisher(t);
            /* create a message for sending */
            Message msg = s_send.createObjectMessage(new Integer(1));
            msg.setIntProperty("size", 2);
            msg.setStringProperty("color", "black");
            //Publish the message
            tpub.publish(msg);
            s_send.commit();

            Message msg1 = ts1.receiveNoWait();
            Message msg2 = ts2.receiveNoWait();
            Message msg3 = ts3.receiveNoWait();

            s_rec1.commit();
            s_rec2.commit();

            //message-properties do not match the selector
            Assert.assertNull(msg1);
            //the other subsrcibers do not use selectors
            Assert.assertNotNull(msg2);
            Assert.assertEquals("msg2 - object", 1, ((Integer)((ObjectMessage)msg2).getObject()).intValue());
            Assert.assertNotNull(msg3);
            Assert.assertEquals("msg3 - object", 1, ((Integer)((ObjectMessage)msg3).getObject()).intValue());

            /* create another message for sending */
            msg = s_send.createObjectMessage(new Integer(88));
            msg.setIntProperty("size", 2);
            msg.setStringProperty("color", "green");
            //Publish the message
            tpub.publish(msg);
            s_send.commit();

            msg1 = ts1.receiveNoWait();
            msg2 = ts2.receiveNoWait();
            msg3 = ts3.receiveNoWait();

            s_rec1.commit();
            s_rec2.commit();

            Assert.assertNotNull(msg1);
            Assert.assertEquals("msg1 - object", 88, ((Integer)((ObjectMessage)msg1).getObject()).intValue());
            Assert.assertNotNull(msg2);
            Assert.assertEquals("msg2 - object", 88, ((Integer)((ObjectMessage)msg2).getObject()).intValue());
            Assert.assertNotNull(msg3);
            Assert.assertEquals("msg3 - object", 88, ((Integer)((ObjectMessage)msg3).getObject()).intValue());
           
        } finally {
            try { s_rec1.close()} catch(Exception ex) {}
            try { s_rec2.close()} catch(Exception ex) {}
            try { s_send.close(); } catch(Exception ex) {}
            try { tc.close();     } catch(Exception ex) {}
        }
    }
View Full Code Here

        Context ctx = new InitialContext(namingProps);
        /* Lookup 'administerable' Objects */
        TopicConnectionFactory tcf = (TopicConnectionFactory)ctx.lookup("TopicConnectionFactory");
        Topic                  t   = (Topic)ctx.lookup("testTopic");

        TopicConnection tc  = null;
        TopicSession s_send = null;
        TopicSession s_rec1 = null;
        TopicSession s_rec2 = null;
        try {
            final long start  = System.currentTimeMillis();
            final String type = Long.toString(start);
            /* Create a connection to the topic */
            tc = tcf.createTopicConnection("system", "system");
            /* Create a session on top of the connection which will be used only for
            sending messages, transacted and with auto-acknowledge-mode*/
            s_send = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
            /* Create sessions on top of the connection which will be used only for
            receiving messages, transacted and with auto-acknowledge-mode */
            s_rec1 = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
            s_rec2 = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
            /* start the connection */
            tc.start();
            /* Create the receivers */
            TopicSubscriber ts1 = s_rec1.createSubscriber(t);
            TopicSubscriber ts2 = s_rec1.createSubscriber(t);
            TopicSubscriber ts3 = s_rec2.createSubscriber(t);
            /* Create a sender for sending messages */
            TopicPublisher tpub = s_send.createPublisher(t);
            tpub.setDisableMessageTimestamp(false);
            /* create a message for sending */
            Message msg = s_send.createObjectMessage(new Integer(123));
            msg.setJMSType(type);
           
            /* Send the message */
            tpub.publish(msg);
            Assert.assertNotNull("sender-JMSMessageID", msg.getJMSMessageID());
            /* Commit the sending session */
            s_send.commit();
            /* Receive the message */
            Message msg1 = ts1.receiveNoWait();
            Message msg2 = ts2.receiveNoWait();
            Message msg3 = ts3.receiveNoWait();
            Assert.assertNotNull("subscriber 1 - message recieved", msg1);
            Assert.assertNotNull("subscriber 2 - message recieved", msg2);
            Assert.assertNotNull("subscriber 3 - message recieved", msg3);
            /* Commit the session 1 to clear the topic */
            s_rec1.commit();
            /* Rollback the session 2 */
            s_rec2.rollback();
            msg3 = ts3.receiveNoWait();
            Assert.assertNotNull("subscriber 3 - message redelivered after rollback", msg3);
            /* Commit the session 2 to clear the topic */
            s_rec2.commit();
            /* Check, if the headers are set for msg1 */
            this.checkHeaders(msg1, t, start, type, false, msg.getJMSMessageID());
            /* Check, if the headers are set for msg2 */
            this.checkHeaders(msg2, t, start, type, false, msg.getJMSMessageID());
            /* Check, if the headers are set for msg3 */
            this.checkHeaders(msg3, t, start, type, true, msg.getJMSMessageID());
        } finally {
            try { s_rec1.close()} catch(Exception ex) {}
            try { s_rec2.close()} catch(Exception ex) {}
            try { s_send.close(); } catch(Exception ex) {}
            try { tc.close();     } catch(Exception ex) {}
        }
    }
View Full Code Here

    {
        /* Create Naming-Context */
        Context ctx = new InitialContext(namingProps);
        /* Lookup 'administerable' Objects */
        TopicConnectionFactory tcf = (TopicConnectionFactory)ctx.lookup("TopicConnectionFactory");
        TopicConnection tc = null;
        TopicSession    ts = null;
        try {
            tc = tcf.createTopicConnection("system", "system");
            ts = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

            String tn = "topic-" + System.currentTimeMillis();
            Topic topic = ts.createTopic(tn);

            Topic t = (Topic)ctx.lookup(tn);
            Assert.assertNotNull("topic-handle", t);
            Assert.assertEquals("topic-handle-name", tn, t.getTopicName());

            TemporaryTopic tt = ts.createTemporaryTopic();

            try {
                ctx.lookup(tt.getTopicName());
                Assert.fail("temporary topic must not be exposed to jndi");
            } catch(javax.naming.NamingException ex) {
                //as expected
            }

            TopicPublisher pub = ts.createPublisher(topic);
            ObjectMessage om = ts.createObjectMessage(new Integer(99));
            pub.publish(om);
            pub.close();

            pub = ts.createPublisher(tt);
            om = ts.createObjectMessage(new Integer(99));
            pub.publish(om);
            pub.close();

        } finally {
            try { ts.close()} catch(Exception ex) {}
            try { tc.close(); } catch(Exception ex) {}
        }
    }
View Full Code Here

        /* Create Naming-Context */
        Context ctx = new InitialContext(namingProps);
        /* Lookup 'administerable' Objects */
        TopicConnectionFactory tcf = (TopicConnectionFactory)ctx.lookup("TopicConnectionFactory");
        Topic topic = (Topic)ctx.lookup("testTopic");
        TopicConnection tc  = null;
        TopicSession    ts  = null;
        try {
            tc = tcf.createTopicConnection("system", "system");
            ts = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

            int num = 1000;
            TopicPublisher tp = ts.createPublisher(topic);
            ObjectMessage om = ts.createObjectMessage(new Integer(num++));
            tp.publish(om);
            om = ts.createObjectMessage(new Integer(num++));
            tp.publish(om);

            TopicSubscriber tsub = ts.createDurableSubscriber(topic, name);
            Message msg = tsub.receiveNoWait();

            Assert.assertNull("initial message", msg);

            om = ts.createObjectMessage(new Integer(num++));
            tp.publish(om);
            om = ts.createObjectMessage(new Integer(num++));
            tp.publish(om);

            msg = tsub.receiveNoWait();
            Assert.assertNotNull("fist message", msg);
            Assert.assertEquals("fist message", new Integer(1002),
                                            ((ObjectMessage)msg).getObject());
            msg = tsub.receiveNoWait();
            Assert.assertNotNull("second message", msg);
            Assert.assertEquals("second message", new Integer(1003),
                                            ((ObjectMessage)msg).getObject());

            tsub.close();

            om = ts.createObjectMessage(new Integer(num++));
            tp.publish(om);
            om = ts.createObjectMessage(new Integer(num++));
            tp.publish(om);

            tsub = ts.createDurableSubscriber(topic, name);

            msg = tsub.receiveNoWait();
            Assert.assertNotNull("third message", msg);
            Assert.assertEquals("third message", new Integer(1004),
                                            ((ObjectMessage)msg).getObject());
            msg = tsub.receiveNoWait();
            Assert.assertNotNull("fourth message", msg);
            Assert.assertEquals("fourth message", new Integer(1005),
                                            ((ObjectMessage)msg).getObject());

            ts.unsubscribe(name);

            om = ts.createObjectMessage(new Integer(num++));
            tp.publish(om);
            om = ts.createObjectMessage(new Integer(num++));
            tp.publish(om);

            tsub = ts.createDurableSubscriber(topic, name);

            msg = tsub.receiveNoWait();
            Assert.assertNull("after unsubscription", msg);

            om = ts.createObjectMessage(new Integer(num++));
            tp.publish(om);

            msg = tsub.receiveNoWait();
            Assert.assertNotNull("fith message", msg);
            Assert.assertEquals("fith message", new Integer(1008),
                                            ((ObjectMessage)msg).getObject());

            ts.unsubscribe(name);

            tsub.close();
            tp.close();

        } finally {
            try { ts.close()} catch(Exception ex) {}
            try { tc.close(); } catch(Exception ex) {}
        }
    }
View Full Code Here

    }

    public void testImplementsQueueAndTopicConnection() throws Exception {
        QueueConnection qc = ((QueueConnectionFactory)connectionFactory).createQueueConnection();
        assertNotNull(qc);
        TopicConnection tc = ((TopicConnectionFactory)connectionFactory).createTopicConnection();
        assertNotNull(tc);
    }
View Full Code Here

                    return connections.get(JMSType.TOPIC);
                }
                else
                {
                    TopicConnectionFactory ccf = (TopicConnectionFactory) createOrReturnConnectionFactory();
                    TopicConnection qc = ccf.createTopicConnection();
                    connections.put(JMSType.TOPIC, qc);

                    return qc;
                }
View Full Code Here

      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);
View Full Code Here

            if (jmsConnectionReusable == null) {
                initJmsSetup();
                // Crude, but currently the only known way to find out
                // whether we're running in server or client container.
                // And we need to know because of J2EE 1.4 spec, J2EE.6.6.
                TopicConnection topCon
                    = topConFacCache.createTopicConnection();
                topCon.start ();
                jmsConnectionReusable = Boolean.FALSE;
                TopicSession ts1 = null;
                TopicSession ts2 = null;
                try {
                    ts1 = topCon.createTopicSession
                        (false, Session.AUTO_ACKNOWLEDGE);
                    ts2 = topCon.createTopicSession
                        (false, Session.AUTO_ACKNOWLEDGE);
                    jmsConnectionReusable = Boolean.TRUE;
                } catch (JMSException e) {
                    // the created topic connection may still be handed out
                    return topCon;
                } finally {
                    if (ts1 != null) {
                        ts1.close();
                    }
                    if (ts2 != null) {
                        ts2.close();
                    }
                }
                topConUnwrappedCache = topCon;
                topConCache = new TopicConnectionWrapper (topCon);
                connectionCleanupThread = new Thread () {
                    public void run () {
                        if (topConUnwrappedCache != null) {
                            try {
                                topConUnwrappedCache.close ();
                            } catch (JMSException e) {
                                logger.warn ("Cannot close - ignored: "
                                             + e.getMessage(), e);
                            }
                            topConCache = null;
                            topConUnwrappedCache = null;
                        }
                    }
                };
                Runtime.getRuntime().addShutdownHook (connectionCleanupThread);
                return topConCache;
            }
  }
  TopicConnection topCon = ((TopicConnectionFactory)topConFacCache)
            .createTopicConnection();
        topCon.start ();
        return topCon;
    }
View Full Code Here

     * @param channel the channel
     * @param message the message
     * @ejb.interface-method view-type="remote"
     */
    public void broadcastChannelMessage (String channel, Map data) {
        TopicConnection tc = null;
        TopicSession ts = null;
        QueueConnection qc = null;
        QueueSession qs = null;
        try {
            tc = topicConnectionFactory().createTopicConnection();
            ts = tc.createTopicSession (true, 0);
            TopicPublisher sndr = ts.createPublisher (channelOutTopic());
            sndr.setDisableMessageID (true);
            ObjectMessage msg
                = ts.createObjectMessage ((Serializable)data);
            String processKey = ((Long)ctx.getPrimaryKey()).toString();
View Full Code Here

TOP

Related Classes of javax.jms.TopicConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.