2012-08-06 85 views
0

我想嘗試一下Apache CXF中的ws-notification示例。他們給出的唯一文件是一個Client.java文件。如何使用Apache CXF運行ws-notification示例?

是我只需要編譯這個Client.java文件,我可以讓程序運行?我嘗試過,但它不工作。

如果不是,將非常感謝任何人可以給我一步一步的指導。

以下是來自ws-notification示例的Client.java的代碼。

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

} 

錯誤是在這裏:

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 [email protected]: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 

回答

0

沒關係,

終於想通了。問題是ws-notification示例應該由maven使用命令行運行。我試圖在日食中運行它們。

當在eclipse中運行時,我們需要添加額外的行來啓動一個WS-Notification服務的實例。

改性代碼段如下:

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");