Search in sources :

Example 1 with Subscription

use of org.apache.cxf.wsn.client.Subscription in project cxf by apache.

the class WsnBrokerTest method testPullPoint.

@Test
public void testPullPoint() throws Exception {
    PullPoint pullPoint = createPullPoint.create();
    Subscription subscription = notificationBroker.subscribe(pullPoint, "myTopic");
    notificationBroker.notify("myTopic", new JAXBElement<String>(new QName("urn:test:org", "foo"), String.class, "bar"));
    boolean received = false;
    for (int i = 0; i < 50; i++) {
        List<NotificationMessageHolderType> messages = pullPoint.getMessages(10);
        if (!messages.isEmpty()) {
            received = true;
            break;
        }
        Thread.sleep(100);
    }
    assertTrue(received);
    subscription.unsubscribe();
    pullPoint.destroy();
}
Also used : QName(javax.xml.namespace.QName) JaxwsCreatePullPoint(org.apache.cxf.wsn.services.JaxwsCreatePullPoint) PullPoint(org.apache.cxf.wsn.client.PullPoint) CreatePullPoint(org.apache.cxf.wsn.client.CreatePullPoint) Subscription(org.apache.cxf.wsn.client.Subscription) NotificationMessageHolderType(org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType) JaxwsCreatePullPoint(org.apache.cxf.wsn.services.JaxwsCreatePullPoint) PullPoint(org.apache.cxf.wsn.client.PullPoint) CreatePullPoint(org.apache.cxf.wsn.client.CreatePullPoint) Test(org.junit.Test)

Example 2 with Subscription

use of org.apache.cxf.wsn.client.Subscription in project cxf by apache.

the class WsnBrokerTest method testRenew.

@Test
public void testRenew() throws Exception {
    TestConsumer callback = new TestConsumer();
    Consumer consumer = new Consumer(callback, "http://localhost:" + port2 + "/test/consumer");
    // create subscription with InitialTerminationTime 2 sec, so that the
    // subscription would be expired after 2 sec
    Subscription subscription = notificationBroker.subscribe(consumer, "myTopic", null, false, "PT02S");
    Thread.sleep(5000);
    synchronized (callback.notifications) {
        System.out.println("send notify");
        notificationBroker.notify("myTopic", new JAXBElement<String>(new QName("urn:test:org", "foo"), String.class, "bar"));
        callback.notifications.wait(2000);
    }
    // the subscription is expired so can't get the notification
    assertEquals(0, callback.notifications.size());
    // renew another 60 sec to resend the notification
    subscription.renew("PT60S");
    synchronized (callback.notifications) {
        notificationBroker.notify("myTopic", new JAXBElement<String>(new QName("urn:test:org", "foo"), String.class, "bar"));
        callback.notifications.wait(10000);
    }
    // the subscription is expired so can't get the notification
    assertEquals(1, callback.notifications.size());
    NotificationMessageHolderType message = callback.notifications.get(0);
    assertEquals(WSNHelper.getInstance().getWSAAddress(subscription.getEpr()), WSNHelper.getInstance().getWSAAddress(message.getSubscriptionReference()));
    subscription.unsubscribe();
    consumer.stop();
}
Also used : Consumer(org.apache.cxf.wsn.client.Consumer) QName(javax.xml.namespace.QName) Subscription(org.apache.cxf.wsn.client.Subscription) NotificationMessageHolderType(org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType) Test(org.junit.Test)

Example 3 with Subscription

use of org.apache.cxf.wsn.client.Subscription in project cxf by apache.

the class WsnBrokerTest method testBroker.

@Test
public void testBroker() throws Exception {
    TestConsumer callback = new TestConsumer();
    Consumer consumer = new Consumer(callback, "http://localhost:" + port2 + "/test/consumer");
    Subscription subscription = notificationBroker.subscribe(consumer, "myTopic");
    synchronized (callback.notifications) {
        notificationBroker.notify("myTopic", new JAXBElement<String>(new QName("urn:test:org", "foo"), String.class, "bar"));
        callback.notifications.wait(1000000);
    }
    assertEquals(1, callback.notifications.size());
    NotificationMessageHolderType message = callback.notifications.get(0);
    assertEquals(WSNHelper.getInstance().getWSAAddress(subscription.getEpr()), WSNHelper.getInstance().getWSAAddress(message.getSubscriptionReference()));
    subscription.unsubscribe();
    consumer.stop();
}
Also used : Consumer(org.apache.cxf.wsn.client.Consumer) QName(javax.xml.namespace.QName) Subscription(org.apache.cxf.wsn.client.Subscription) NotificationMessageHolderType(org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType) Test(org.junit.Test)

Example 4 with Subscription

use of org.apache.cxf.wsn.client.Subscription in project cxf by apache.

the class WsnBrokerTest method testPublisherCustomType.

@Test
public void testPublisherCustomType() throws Exception {
    notificationBroker.setExtraClasses(CustomType.class);
    TestConsumer consumerCallback = new TestConsumer();
    Consumer consumer = new Consumer(consumerCallback, "http://localhost:" + port2 + "/test/consumer", CustomType.class);
    Subscription subscription = notificationBroker.subscribe(consumer, "myTopic");
    PublisherCallback publisherCallback = new PublisherCallback();
    Publisher publisher = new Publisher(publisherCallback, "http://localhost:" + port2 + "/test/publisher");
    Registration registration = notificationBroker.registerPublisher(publisher, "myTopic");
    synchronized (consumerCallback.notifications) {
        notificationBroker.notify(publisher, "myTopic", new CustomType(1, 2));
        consumerCallback.notifications.wait(1000000);
    }
    assertEquals(1, consumerCallback.notifications.size());
    NotificationMessageHolderType message = consumerCallback.notifications.get(0);
    assertEquals(WSNHelper.getInstance().getWSAAddress(subscription.getEpr()), WSNHelper.getInstance().getWSAAddress(message.getSubscriptionReference()));
    assertEquals(WSNHelper.getInstance().getWSAAddress(publisher.getEpr()), WSNHelper.getInstance().getWSAAddress(message.getProducerReference()));
    assertNotNull(message.getMessage().getAny());
    assertTrue(message.getMessage().getAny().getClass().getName(), message.getMessage().getAny() instanceof CustomType);
    subscription.unsubscribe();
    registration.destroy();
    publisher.stop();
    consumer.stop();
}
Also used : CustomType(org.apache.cxf.wsn.types.CustomType) Consumer(org.apache.cxf.wsn.client.Consumer) Registration(org.apache.cxf.wsn.client.Registration) Publisher(org.apache.cxf.wsn.client.Publisher) Subscription(org.apache.cxf.wsn.client.Subscription) NotificationMessageHolderType(org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType) Test(org.junit.Test)

Example 5 with Subscription

use of org.apache.cxf.wsn.client.Subscription in project cxf by apache.

the class Client method main.

/**
 * @param args
 */
public static void main(String[] args) throws Exception {
    String wsnPort = "9000";
    if (args.length > 0) {
        wsnPort = args[0];
    }
    // Start a consumer that will listen for notification messages
    // We'll just print the text content out for now.
    Consumer consumer = new Consumer(new Consumer.Callback() {

        public void notify(NotificationMessageHolderType message) {
            Object o = message.getMessage().getAny();
            System.out.println(message.getMessage().getAny());
            if (o instanceof Element) {
                System.out.println(((Element) o).getTextContent());
            }
        }
    }, "http://localhost:9001/MyConsumer");
    // Create a subscription for a Topic on the broker
    NotificationBroker notificationBroker = new NotificationBroker("http://localhost:" + wsnPort + "/wsn/NotificationBroker");
    Subscription subscription = notificationBroker.subscribe(consumer, "MyTopic");
    // Send a notification on the Topic
    notificationBroker.notify("MyTopic", new JAXBElement<String>(new QName("urn:test:org", "foo"), String.class, "Hello World!"));
    // Just sleep for a bit to make sure the notification gets delivered
    Thread.sleep(5000);
    // Cleanup and exit
    subscription.unsubscribe();
    consumer.stop();
    System.exit(0);
}
Also used : Consumer(org.apache.cxf.wsn.client.Consumer) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) JAXBElement(javax.xml.bind.JAXBElement) NotificationMessageHolderType(org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType) Subscription(org.apache.cxf.wsn.client.Subscription) NotificationBroker(org.apache.cxf.wsn.client.NotificationBroker)

Aggregations

Subscription (org.apache.cxf.wsn.client.Subscription)9 QName (javax.xml.namespace.QName)8 Test (org.junit.Test)8 NotificationMessageHolderType (org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType)8 Consumer (org.apache.cxf.wsn.client.Consumer)7 Publisher (org.apache.cxf.wsn.client.Publisher)4 Registration (org.apache.cxf.wsn.client.Registration)4 CreatePullPoint (org.apache.cxf.wsn.client.CreatePullPoint)2 PullPoint (org.apache.cxf.wsn.client.PullPoint)2 JaxwsCreatePullPoint (org.apache.cxf.wsn.services.JaxwsCreatePullPoint)2 JAXBElement (javax.xml.bind.JAXBElement)1 NotificationBroker (org.apache.cxf.wsn.client.NotificationBroker)1 CustomType (org.apache.cxf.wsn.types.CustomType)1 Element (org.w3c.dom.Element)1