J'ai retrouvé mon exemple asynchrone avec JAX-WS
Il faut commencer par généré les classes "client" pour les web service (ou alors passer par un Dispatch mais c'est une autre histoire)
Pour généré les classes client pour un web service disponible sur http://localhost:9083/hello?wsdl :
wsimport -s D:\eclipse\Test\TestJaxWSClientAsynch\src -b D:\eclipse\Test\TestJaxWSClientAsynch\src\asynch\bindings.xml http://localhost:9083/hello?wsdl
Voici le fichier bindings.xml que j'ai utilisé :
1 2 3 4 5 6 7
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<bindings
wsdlLocation="http://localhost:9083/hello?wsdl"
xmlns="http://java.sun.com/xml/ns/jaxws">
<enableAsyncMapping>true</enableAsyncMapping>
</bindings> |
Une fois les classes générée, mon "main" ressemble à cela :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
package asynch;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.xml.namespace.QName;
import javax.xml.ws.Response;
import javax.xml.ws.Service;
import oca.MonPortType; // le package oca est généré par wsimport
import oca.SayHelloResponse; // le package oca est généré par wsimport
public class ASynchClient {
public static void main(String args[]) throws Exception {
URL wsdlLocation = null;
try {
wsdlLocation = new URL("http://localhost:9083/hello?wsdl");
} catch (MalformedURLException e) {
e.printStackTrace();
}
QName serviceName = new QName("http://oca", "MonService");
Service s = Service.create(wsdlLocation, serviceName);
MonPortType port = s.getPort(MonPortType.class);
ExecutorService executor = Executors.newFixedThreadPool(5);
s.setExecutor(executor);
// par Polling
Response<SayHelloResponse> resp = port.sayHelloAsync("Hi", "Oli");
while(!resp.isDone()){
System.out.println("waiting");
}
SayHelloResponse sayRep = resp.get();
System.out.println(sayRep.getReturn());
// par Callback
port.sayHelloAsync("Hello","Oli",new MyAsyncHandler());
System.out.println("=====================================");
System.out.println("Completed");
System.exit(0);
}
} |
Il faut encore ceci :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
package asynch;
import java.util.concurrent.ExecutionException;
import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response;
import oca.SayHelloResponse; // le package oca est généré par wsimport
public class MyAsyncHandler implements AsyncHandler<SayHelloResponse>{
@Override
public void handleResponse(Response<SayHelloResponse> res){
try {
SayHelloResponse resp = res.get();
System.out.println("Event -> "+resp.getReturn());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
} |
A noter que le main test les deux méthodes disponibles dans JAX-WS pour un appel asynchrone , à savoir le callback et le polling.
Voila, j'espère que cela peut t'aider
A+
Partager