Search in sources :

Example 1 with Notify

use of org.oasis_open.docs.wsn.b_2.Notify in project cxf by apache.

the class NotificationBroker method notify.

public void notify(Referencable publisher, String topic, Object msg) {
    getBroker();
    if (this.context != null) {
        try {
            DOMResult result = new DOMResult();
            context.createMarshaller().marshal(msg, result);
            msg = result.getNode();
            if (msg instanceof Document) {
                msg = ((Document) msg).getDocumentElement();
            }
        } catch (JAXBException e) {
        // ignore, we'll try and let the runtime handle it as is
        }
    }
    Notify notify = new Notify();
    NotificationMessageHolderType holder = new NotificationMessageHolderType();
    if (publisher != null) {
        holder.setProducerReference(publisher.getEpr());
    }
    if (topic != null) {
        TopicExpressionType topicExp = new TopicExpressionType();
        topicExp.getContent().add(topic);
        holder.setTopic(topicExp);
    }
    holder.setMessage(new NotificationMessageHolderType.Message());
    holder.getMessage().setAny(msg);
    notify.getNotificationMessage().add(holder);
    getBroker().notify(notify);
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) Notify(org.oasis_open.docs.wsn.b_2.Notify) TopicExpressionType(org.oasis_open.docs.wsn.b_2.TopicExpressionType) JAXBException(javax.xml.bind.JAXBException) Document(org.w3c.dom.Document) NotificationMessageHolderType(org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType)

Example 2 with Notify

use of org.oasis_open.docs.wsn.b_2.Notify in project cxf by apache.

the class JmsPullPoint method getMessages.

@Override
protected List<NotificationMessageHolderType> getMessages(int max) throws ResourceUnknownFault, UnableToGetMessagesFault {
    try {
        if (max == 0) {
            max = 256;
        }
        initSession();
        List<NotificationMessageHolderType> messages = new ArrayList<>();
        for (int i = 0; i < max; i++) {
            Message msg = null;
            synchronized (consumerSession) {
                msg = consumer.receiveNoWait();
            }
            if (msg == null) {
                break;
            }
            TextMessage txtMsg = (TextMessage) msg;
            StringReader reader = new StringReader(txtMsg.getText());
            XMLStreamReader xreader = StaxUtils.createXMLStreamReader(reader);
            Notify notify = (Notify) jaxbContext.createUnmarshaller().unmarshal(xreader);
            try {
                xreader.close();
            } catch (XMLStreamException e) {
            // ignoreable
            }
            messages.addAll(notify.getNotificationMessage());
        }
        return messages;
    } catch (JMSException e) {
        LOGGER.log(Level.INFO, "Error retrieving messages", e);
        closeSession();
        UnableToGetMessagesFaultType fault = new UnableToGetMessagesFaultType();
        throw new UnableToGetMessagesFault("Unable to retrieve messages", fault, e);
    } catch (JAXBException e) {
        LOGGER.log(Level.INFO, "Error retrieving messages", e);
        UnableToGetMessagesFaultType fault = new UnableToGetMessagesFaultType();
        throw new UnableToGetMessagesFault("Unable to retrieve messages", fault, e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Notify(org.oasis_open.docs.wsn.b_2.Notify) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JMSException(javax.jms.JMSException) NotificationMessageHolderType(org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType) AbstractPullPoint(org.apache.cxf.wsn.AbstractPullPoint) UnableToGetMessagesFaultType(org.oasis_open.docs.wsn.b_2.UnableToGetMessagesFaultType) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) UnableToGetMessagesFault(org.oasis_open.docs.wsn.bw_2.UnableToGetMessagesFault) TextMessage(javax.jms.TextMessage)

Example 3 with Notify

use of org.oasis_open.docs.wsn.b_2.Notify in project cxf by apache.

the class JmsSubscription method onMessage.

public void onMessage(Message jmsMessage) {
    try {
        TextMessage text = (TextMessage) jmsMessage;
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(new StringReader(text.getText()));
        Notify notify = (Notify) jaxbContext.createUnmarshaller().unmarshal(reader);
        reader.close();
        for (Iterator<NotificationMessageHolderType> ith = notify.getNotificationMessage().iterator(); ith.hasNext(); ) {
            NotificationMessageHolderType h = ith.next();
            Object content = h.getMessage().getAny();
            if (!(content instanceof Element)) {
                DocumentFragment doc = DOMUtils.getEmptyDocument().createDocumentFragment();
                jaxbContext.createMarshaller().marshal(content, doc);
                content = DOMUtils.getFirstElement(doc);
            }
            if (!doFilter((Element) content)) {
                ith.remove();
            } else {
                h.setTopic(topic);
                h.setSubscriptionReference(getEpr());
            }
        }
        if (!notify.getNotificationMessage().isEmpty()) {
            doNotify(notify);
        }
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "Error notifying consumer", e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) Notify(org.oasis_open.docs.wsn.b_2.Notify) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) NotificationMessageHolderType(org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType) TextMessage(javax.jms.TextMessage) DocumentFragment(org.w3c.dom.DocumentFragment) XPathExpressionException(javax.xml.xpath.XPathExpressionException) JMSException(javax.jms.JMSException) JAXBException(javax.xml.bind.JAXBException)

Example 4 with Notify

use of org.oasis_open.docs.wsn.b_2.Notify 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 5 with Notify

use of org.oasis_open.docs.wsn.b_2.Notify 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

Notify (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Notify)11 Test (org.junit.Test)9 NotificationMessageHolderType (org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType)7 JAXBException (javax.xml.bind.JAXBException)6 Notify (org.oasis_open.docs.wsn.b_2.Notify)5 Open (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open)5 Notification (org.opendaylight.yangtools.yang.binding.Notification)5 JMSException (javax.jms.JMSException)4 OpenBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.OpenBuilder)4 Message (javax.jms.Message)3 TextMessage (javax.jms.TextMessage)3 Ipv4Address (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address)3 ProtocolVersion (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.ProtocolVersion)3 Element (org.w3c.dom.Element)3 ByteBuf (io.netty.buffer.ByteBuf)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 QName (javax.xml.namespace.QName)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 Consumer (org.apache.cxf.wsn.client.Consumer)2