Search in sources :

Example 1 with TopicSubscriber

use of javax.jms.TopicSubscriber in project ACS by ACS-Community.

the class PubSubTest method testTopicConnection.

public void testTopicConnection() throws Exception {
    TopicConnection topicConnection = factory.createTopicConnection();
    assertNotNull("Error creating the TopicConnection", topicConnection);
    TopicSession topicSession = topicConnection.createTopicSession(true, 0);
    assertNotNull("Error creating the TopicSession", topicSession);
    String chName = "TOPIC.TEST";
    Topic topic = topicSession.createTopic(chName);
    assertNotNull("Error creating Topic", topic);
    assertEquals("Wrong name for the topic", chName, topic.getTopicName());
    TopicPublisher publisher = topicSession.createPublisher(topic);
    assertNotNull("Error creating the TopicPublisher ", publisher);
    TopicSubscriber subscriber = topicSession.createSubscriber(topic);
    assertNotNull("Error creating the TopicSubscriber", subscriber);
    subscriber.setMessageListener(this);
    ACSJMSTextMessage msg = new ACSJMSTextMessage(getContainerServices());
    assertNotNull("Error creating ACSJMSTextMessage", msg);
    msg.setText(msgToSend);
    publisher.publish(msg);
    // Wait for awhile to have enough time to receive the message
    try {
        Thread.sleep(10000);
    } catch (Exception e) {
    }
    publisher.close();
    subscriber.close();
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) TopicSession(javax.jms.TopicSession) TopicPublisher(javax.jms.TopicPublisher) ACSJMSTextMessage(com.cosylab.acs.jms.ACSJMSTextMessage) Topic(javax.jms.Topic) TopicConnection(javax.jms.TopicConnection)

Example 2 with TopicSubscriber

use of javax.jms.TopicSubscriber in project ACS by ACS-Community.

the class DefaultSubscriberImpl method subscribe.

/**
   * Method subscribe
   *
   *
   * @param topic
   * @param listener
   * @param selector
   *
   * @return long the subscription handle identifier
   *
   * @throws JMSException
   * @throws NamingException
   *
   */
public long subscribe(String topic, SubscriptionListener listener, String selector) throws JMSException, NamingException {
    cat.info("subscribe(" + topic + ", listener, " + selector + ")");
    if (closed) {
        throw (new JMSException("Subscriber closed."));
    }
    SubscriptionHandle handle = new SubscriptionHandle();
    handle.setSubscriptionTopic(topic);
    handle.setSubscriptionSelector(selector);
    handle.setSubscriptionListener(listener);
    StringBuffer local_selector = new StringBuffer();
    if (NotificationHelper.isNotification(topic)) {
        // this is a subscription to notifications, no further selection is required
        local_selector.append(selector);
    } else {
        // subscription to a generic topic, adding subscriber specific selection
        if (selector != null) {
            local_selector.append(selector);
            local_selector.append(" AND ");
        }
        local_selector.append("( (");
        local_selector.append(NotificationHelper.SUBSCRIPTION_ID_PROPERTY);
        local_selector.append(" IS NULL) OR (");
        local_selector.append(NotificationHelper.SUBSCRIPTION_ID_PROPERTY);
        local_selector.append(" = '");
        local_selector.append(subscriberId);
        local_selector.append("@");
        local_selector.append(handle.getSubscriptionToken());
        local_selector.append("') )");
    }
    TopicSession session = null;
    TopicSubscriber subscriber = null;
    Topic the_topic = createTopic(topic);
    try {
        session = connection.createTopicSession();
        subscriber = session.createSubscriber(the_topic, local_selector.toString(), false);
    } catch (JMSSecurityException jse) {
        cat.error("JMSSecurityException caught");
        throw (new NamingException(jse.getMessage()));
    } catch (JMSException je) {
        cat.error("JMSException caught");
        throw (new NamingException(je.getMessage()));
    } catch (ConnectionException ce) {
        cat.error("ConnectionException caught");
        throw (new JMSException(ce.getMessage()));
    } catch (Exception e) {
        cat.error("Generic exception caught", e);
    }
    subscriber.setMessageListener(listener);
    handle.setSubscriber(subscriber);
    handle.setSession(session);
    synchronized (subscribers) {
        subscribers.put(new Long(handle.getSubscriptionToken()), handle);
    }
    if (!NotificationHelper.isNotification(topic)) {
        publishNotification(NotificationHelper.CONSUMER_OPEN_NOTIFICATION, handle);
    }
    return handle.getSubscriptionToken();
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) TopicSession(javax.jms.TopicSession) JMSSecurityException(javax.jms.JMSSecurityException) JMSException(javax.jms.JMSException) NamingException(javax.naming.NamingException) Topic(javax.jms.Topic) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) MOMException(cern.cmw.mom.pubsub.MOMException) JMSSecurityException(javax.jms.JMSSecurityException)

Example 3 with TopicSubscriber

use of javax.jms.TopicSubscriber in project ACS by ACS-Community.

the class DefaultSubscriberImpl method recoverSubscriptions.

/**
   * Method recoverSubscriptions
   *
   */
protected void recoverSubscriptions() {
    cat.info("recoverSubscriptions()");
    try {
        SubscriptionHandle handle = null;
        Iterator iterator = subscribers.values().iterator();
        while (iterator.hasNext()) {
            handle = (SubscriptionHandle) iterator.next();
            if (!NotificationHelper.isNotification(handle.getSubscriptionTopic())) {
                publishNotification(NotificationHelper.CONSUMER_CLOSE_NOTIFICATION, handle);
            }
            cat.debug("Recovering subscription to : " + handle.getSubscriptionTopic());
            TopicSession session = connection.createTopicSession();
            TopicSubscriber subscriber = session.createSubscriber(createTopic(handle.getSubscriptionTopic()), handle.getSubscriptionSelector(), false);
            subscriber.setMessageListener(handle.getSubscriptionListener());
            handle.setSession(session);
            handle.setSubscriber(subscriber);
            if (!NotificationHelper.isNotification(handle.getSubscriptionTopic())) {
                publishNotification(NotificationHelper.CONSUMER_OPEN_NOTIFICATION, handle);
            }
        }
        cat.info("Subscriptions succesfully recovered.");
    } catch (Exception e) {
        cat.error("unable to recover subscriptions", e);
    }
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) TopicSession(javax.jms.TopicSession) Iterator(java.util.Iterator) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) MOMException(cern.cmw.mom.pubsub.MOMException) JMSSecurityException(javax.jms.JMSSecurityException)

Example 4 with TopicSubscriber

use of javax.jms.TopicSubscriber in project wildfly by wildfly.

the class JMSTopicManagementTestCase method removeJMSTopicRemovesAllMessages.

@Test
public void removeJMSTopicRemovesAllMessages() throws Exception {
    // create a durable subscriber
    final String subscriptionName = "removeJMSTopicRemovesAllMessages";
    // stop the consumer connection to prevent eager consumption of messages
    consumerConn.stop();
    TopicSubscriber consumer = consumerSession.createDurableSubscriber(topic, subscriptionName);
    MessageProducer producer = session.createProducer(topic);
    producer.send(session.createTextMessage("A"));
    ModelNode operation = getTopicOperation("count-messages-for-subscription");
    operation.get("client-id").set(consumerConn.getClientID());
    operation.get("subscription-name").set(subscriptionName);
    ModelNode result = execute(operation, true);
    assertTrue(result.isDefined());
    Assert.assertEquals(1, result.asInt());
    // remove the topic
    adminSupport.removeJmsTopic(getTopicName());
    // add the topic
    adminSupport.createJmsTopic(getTopicName(), getTopicJndiName());
    // and recreate the durable subscriber to check all the messages have
    // been removed from the topic
    consumerSession.createDurableSubscriber(topic, subscriptionName);
    result = execute(operation, true);
    assertTrue(result.isDefined());
    Assert.assertEquals(0, result.asInt());
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) MessageProducer(javax.jms.MessageProducer) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Aggregations

TopicSubscriber (javax.jms.TopicSubscriber)4 TopicSession (javax.jms.TopicSession)3 MOMException (cern.cmw.mom.pubsub.MOMException)2 JMSException (javax.jms.JMSException)2 JMSSecurityException (javax.jms.JMSSecurityException)2 Topic (javax.jms.Topic)2 NamingException (javax.naming.NamingException)2 ACSJMSTextMessage (com.cosylab.acs.jms.ACSJMSTextMessage)1 Iterator (java.util.Iterator)1 MessageProducer (javax.jms.MessageProducer)1 TopicConnection (javax.jms.TopicConnection)1 TopicPublisher (javax.jms.TopicPublisher)1 ModelNode (org.jboss.dmr.ModelNode)1 Test (org.junit.Test)1