Commit a930bf1f by Patryk Czarnik

Programy Async Dispatch

parent ba401de1
package ogloszenia.klient_niskopoziomowy;
import jakarta.xml.ws.Dispatch;
import jakarta.xml.ws.Response;
import jakarta.xml.ws.Service;
import ogloszenia.generated.OgloszeniaService;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.util.concurrent.ExecutionException;
public class Async1 {
static final String trescXML = """
<o:readOne xmlns:o="http://soap.ogloszenia/">
<id>1</id>
</o:readOne>""";
public static void main(String[] args) {
System.out.println("Startujemy...");
OgloszeniaService service = new OgloszeniaService();
QName portName = OgloszeniaService.OgloszeniaPort;
Dispatch<Source> dispatch = service.createDispatch(portName, Source.class, Service.Mode.PAYLOAD);
System.out.println("Mam Dispatchera " + dispatch);
Utils utils = new Utils();
Source src = new StreamSource(new StringReader(trescXML));
System.out.println("Wysyłam zapytanie");
Response<Source> response = dispatch.invokeAsync(src);
System.out.println("Zapytanie zlecone");
System.out.println("lalalala, robię inne rzeczy");
boolean done = false;
do {
utils.czekaj(1000);
done = response.isDone();
System.out.println("Czy wynik już jest? " + done);
} while (!done);
try {
Source result = response.get();
System.out.println("Wykonuje to wątek nr " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
System.out.println("Mam wynik: " + result);
utils.wypiszTransformerem(result);
System.out.println("Gotowe");
} catch (InterruptedException e) {
System.err.println(e);
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
package ogloszenia.klient_niskopoziomowy;
import jakarta.xml.ws.Dispatch;
import jakarta.xml.ws.Response;
import jakarta.xml.ws.Service;
import ogloszenia.generated.OgloszeniaService;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.util.concurrent.ExecutionException;
public class Async2 {
static final String trescXML = """
<o:readOne xmlns:o="http://soap.ogloszenia/">
<id>1</id>
</o:readOne>""";
public static void main(String[] args) {
System.out.println("Startujemy...");
OgloszeniaService service = new OgloszeniaService();
QName portName = OgloszeniaService.OgloszeniaPort;
Dispatch<Source> dispatch = service.createDispatch(portName, Source.class, Service.Mode.PAYLOAD);
System.out.println("Mam Dispatchera " + dispatch);
Utils utils = new Utils();
Source src = new StreamSource(new StringReader(trescXML));
System.out.println("Wysyłam zapytanie");
dispatch.invokeAsync(src, response -> {
try {
Source result = response.get();
System.out.println("Wykonuje to wątek nr " + Thread.currentThread().getId() + " " + Thread.currentThread().getName());
System.out.println("Mam wynik: " + result);
utils.wypiszTransformerem(result);
System.out.println("Gotowe");
} catch (InterruptedException e) {
System.err.println(e);
} catch (ExecutionException e) {
e.printStackTrace();
}
});
System.out.println("Zapytanie zlecone");
System.out.println("lalalala, robię inne rzeczy");
utils.czekaj(1000);
System.out.println("Koniec programu");
}
}
...@@ -28,4 +28,12 @@ public class Utils { ...@@ -28,4 +28,12 @@ public class Utils {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public void czekaj(long milis) {
try {
Thread.sleep(milis);
} catch (InterruptedException e) {
System.err.println(e);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment