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);
}
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);
}
}
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);
}
}
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();
}
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);
}
Aggregations