Question

I would like to try out the WS-Notification example in the Apache CXF. the only file they give is a Client.java file.

Is it that I just need to compile this Client.java file and I can get the program running? i tried but it not working.

If not, will greatly appreciate anyone that can give me a step by step guide.

Below is the code of Client.java from WS-Notification sample.

package demo.client;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

import org.w3c.dom.Element;

import org.apache.cxf.wsn.client.Consumer;
import org.apache.cxf.wsn.client.NotificationBroker;
import org.apache.cxf.wsn.client.Subscription;
import org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType;

/**
 * 
 */
public final class Client {
private Client() {
    //not constructed
}

/**
 * @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);
}

}

Error is here:

Aug 10, 2012 4:52:50 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://client.wsn.cxf.apache.org/}ConsumerService from class org.oasis_open.docs.wsn.bw_2.NotificationConsumer
Aug 10, 2012 4:52:52 PM org.apache.cxf.endpoint.ServerImpl initDestination
INFO: Setting the server's publish address to be http://localhost:9001/MyConsumer
Aug 10, 2012 4:52:52 PM org.eclipse.jetty.server.Server doStart
INFO: jetty-7.5.4.v20111024
Aug 10, 2012 4:52:52 PM org.eclipse.jetty.server.AbstractConnector doStart
INFO: Started SelectChannelConnector@localhost:9001 STARTING
Aug 10, 2012 4:52:52 PM org.eclipse.jetty.server.handler.ContextHandler startContext
INFO: started o.e.j.s.h.ContextHandler{,null}
Aug 10, 2012 4:52:52 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
INFO: Creating Service {http://cxf.apache.org/wsn/jaxws}NotificationBrokerService from WSDL: jar:file:/home/wenbin/Desktop/keith/Apache/apache-cxf-2.6.1/lib/cxf-services-wsn-api-2.6.1.jar!/org/apache/cxf/wsn/wsdl/wsn.wsdl
Aug 10, 2012 4:52:53 PM org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
WARNING: Interceptor for {http://cxf.apache.org/wsn/jaxws}NotificationBrokerService#{http://docs.oasis-open.org/wsn/brw-2}Subscribe has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: Could not send Message.
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:532)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:464)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:367)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:320)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:89)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
    at $Proxy66.subscribe(Unknown Source)
    at org.apache.cxf.wsn.client.NotificationBroker.subscribe(NotificationBroker.java:208)
    at org.apache.cxf.wsn.client.NotificationBroker.subscribe(NotificationBroker.java:162)
    at test.Client.main(Client.java:67)
Caused by: java.net.ConnectException: ConnectException invoking http://localhost:9000/wsn/NotificationBroker: Connection refused
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.mapException(HTTPConduit.java:1457)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1442)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:658)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    ... 11 more
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
    at java.net.Socket.connect(Socket.java:546)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:173)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:409)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:240)
    at sun.net.www.http.HttpClient.New(HttpClient.java:321)
    at sun.net.www.http.HttpClient.New(HttpClient.java:338)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:935)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:876)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:801)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:979)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleHeadersTrustCaching(HTTPConduit.java:1394)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.onFirstWrite(HTTPConduit.java:1336)
    at org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:42)
    at org.apache.cxf.io.AbstractThresholdOutputStream.write(AbstractThresholdOutputStream.java:69)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1414)
    ... 14 more
Exception in thread "main" javax.xml.ws.WebServiceException: Could not send Message.
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)
    at $Proxy66.subscribe(Unknown Source)
    at org.apache.cxf.wsn.client.NotificationBroker.subscribe(NotificationBroker.java:208)
    at org.apache.cxf.wsn.client.NotificationBroker.subscribe(NotificationBroker.java:162)
    at test.Client.main(Client.java:67)
Caused by: java.net.ConnectException: ConnectException invoking http://localhost:9000/wsn/NotificationBroker: Connection refused
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.mapException(HTTPConduit.java:1457)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1442)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:658)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:532)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:464)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:367)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:320)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:89)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
    ... 4 more
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
    at java.net.Socket.connect(Socket.java:546)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:173)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:409)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:240)
    at sun.net.www.http.HttpClient.New(HttpClient.java:321)
    at sun.net.www.http.HttpClient.New(HttpClient.java:338)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:935)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:876)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:801)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:979)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleHeadersTrustCaching(HTTPConduit.java:1394)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.onFirstWrite(HTTPConduit.java:1336)
    at org.apache.cxf.io.AbstractWrappedOutputStream.write(AbstractWrappedOutputStream.java:42)
    at org.apache.cxf.io.AbstractThresholdOutputStream.write(AbstractThresholdOutputStream.java:69)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1414)
    ... 14 more
Was it helpful?

Solution

okay,

finally figured it out. the issue is the ws-notification sample should be run by maven using the command line. i was trying to run them in eclipse.

when running in eclipse, we will need to add in extra line to Starts up an instance of a WS-Notification service.

the modified codes segment as below:

NotificationBroker notificationBroker 
    = new NotificationBroker("http://localhost:" + wsnPort + "/wsn/NotificationBroker");

//these 3 lines are the codes added.
String agu[] = new String[2];       
Service service = new Service(agu);
service.start();

Subscription subscription = notificationBroker.subscribe(consumer, "MyTopic");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top