1

我想试试 Apache CXF 中的 WS-Notification 示例。他们提供的唯一文件是 Client.java 文件。

是不是我只需要编译这个 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 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
4

1 回答 1

1

好的,

终于想通了。问题是 ws-notification 示例应该由 maven 使用命令行运行。我试图在 Eclipse 中运行它们。

在 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");
于 2012-08-14T05:39:04.167 回答