Commit c30e795b by Patryk Czarnik

programy klienckie w wersji asynchronicznej

parent 14683884
<bindings xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="https://jakarta.ee/xml/ns/jaxws">
<bindings node="wsdl:portType[@name='Sklep']/wsdl:operation[@name='readProductsByPrice']">
<enableAsyncMapping>true</enableAsyncMapping>
</bindings>
<!-- wersja obejmująca wszystkie operacje:
<bindings node="wsdl:portType[@name='Sklep']/wsdl:operation">
<enableAsyncMapping>true</enableAsyncMapping>
</bindings>
-->
</bindings>
......@@ -17,3 +17,11 @@ wersja Linux:
wersja Windows - przykładowo:
C:\Tools\wildfly-35.0.1.Final\bin\wsconsume.bat -k -n -s src\main\java -p sklep.generated "http://localhost:8080/PC25-SoapSerwer/Sklep?wsdl"
Dodatkowe opcje dla tego generowania można opisać w pliku (tuaj async-binding.xml)
zgodnym ze schemą
https://jakarta.ee/xml/ns/jaxws/wsdl_customizationschema_3_0.xsd
i wskazać ten plik z opcją -b
/opt/java/wildfly-35.0.1.Final/bin/wsconsume.sh -k -n -s src/main/java -b async-binding.xml -p sklep.generated "http://localhost:8080/PC24-SklepSoap/Sklep?wsdl"
......@@ -6,7 +6,7 @@ import jakarta.xml.ws.WebFault;
/**
* This class was generated by Apache CXF 4.0.6
* 2025-05-10T16:48:10.040+02:00
* 2025-05-11T10:17:25.006+02:00
* Generated source version: 4.0.6
*/
......
......@@ -6,7 +6,7 @@ import jakarta.xml.ws.WebFault;
/**
* This class was generated by Apache CXF 4.0.6
* 2025-05-10T16:48:09.949+02:00
* 2025-05-11T10:17:24.930+02:00
* Generated source version: 4.0.6
*/
......
......@@ -5,12 +5,15 @@ import jakarta.jws.WebParam;
import jakarta.jws.WebResult;
import jakarta.jws.WebService;
import jakarta.xml.bind.annotation.XmlSeeAlso;
import jakarta.xml.ws.AsyncHandler;
import jakarta.xml.ws.RequestWrapper;
import jakarta.xml.ws.Response;
import jakarta.xml.ws.ResponseWrapper;
import java.util.concurrent.Future;
/**
* This class was generated by Apache CXF 4.0.6
* 2025-05-10T16:48:10.059+02:00
* 2025-05-11T10:17:25.033+02:00
* Generated source version: 4.0.6
*
*/
......@@ -59,6 +62,30 @@ public interface Sklep {
int id
) throws RecordNotFound_Exception, DBException_Exception;
@WebMethod(operationName = "readProductsByPrice")
@RequestWrapper(localName = "readProductsByPrice", targetNamespace = "http://soap.sklep/", className = "sklep.generated.ReadProductsByPrice")
@ResponseWrapper(localName = "readProductsByPriceResponse", targetNamespace = "http://soap.sklep/", className = "sklep.generated.ReadProductsByPriceResponse")
public Response<sklep.generated.ReadProductsByPriceResponse> readProductsByPriceAsync(
@WebParam(name = "min", targetNamespace = "")
java.math.BigDecimal min,
@WebParam(name = "max", targetNamespace = "")
java.math.BigDecimal max
);
@WebMethod(operationName = "readProductsByPrice")
@ResponseWrapper(localName = "readProductsByPriceResponse", targetNamespace = "http://soap.sklep/", className = "sklep.generated.ReadProductsByPriceResponse")
@RequestWrapper(localName = "readProductsByPrice", targetNamespace = "http://soap.sklep/", className = "sklep.generated.ReadProductsByPrice")
public Future<?> readProductsByPriceAsync(
@WebParam(name = "min", targetNamespace = "")
java.math.BigDecimal min,
@WebParam(name = "max", targetNamespace = "")
java.math.BigDecimal max,
@WebParam(name = "asyncHandler", targetNamespace = "")
AsyncHandler<sklep.generated.ReadProductsByPriceResponse> asyncHandler
);
@WebMethod
@RequestWrapper(localName = "readProductsByPrice", targetNamespace = "http://soap.sklep/", className = "sklep.generated.ReadProductsByPrice")
@ResponseWrapper(localName = "readProductsByPriceResponse", targetNamespace = "http://soap.sklep/", className = "sklep.generated.ReadProductsByPriceResponse")
......
......@@ -11,7 +11,7 @@ import jakarta.xml.ws.Service;
/**
* This class was generated by Apache CXF 4.0.6
* 2025-05-10T16:48:10.098+02:00
* 2025-05-11T10:17:25.066+02:00
* Generated source version: 4.0.6
*
*/
......
package sklep.klient;
import java.math.BigDecimal;
import java.util.List;
import jakarta.xml.ws.Response;
import sklep.generated.Product;
import sklep.generated.ReadProductsByPriceResponse;
import sklep.generated.Sklep;
import sklep.generated.SklepService;
public class Klient4_Async {
public static void main(String[] args) {
BigDecimal min = BigDecimal.valueOf(2000);
BigDecimal max = BigDecimal.valueOf(3000);
System.out.println("Początek programu");
SklepService service = new SklepService();
Sklep sklep = service.getSklepPort();
System.out.println("Klient gotowy do pracy");
try {
System.out.println("Wysyłam zapytanie");
Response<ReadProductsByPriceResponse> response = sklep.readProductsByPriceAsync(min, max);
System.out.println("Wysłałem zapytanie i mam obiekt reponse: " + response);
// strategia odpytywania:
while(! response.isDone()) {
System.out.println("czekam na wynik");
Thread.sleep(10);
}
List<Product> products = response.get().getProduct();
for(Product product : products) {
System.out.println(product.getProductName() + " za " + product.getPrice());
}
} catch (Exception e) {
System.out.println("Błąd: " + e);
}
System.out.println("Koniec programu");
}
}
package sklep.klient;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import jakarta.xml.ws.Response;
import sklep.generated.Product;
import sklep.generated.ReadProductsByPriceResponse;
import sklep.generated.Sklep;
import sklep.generated.SklepService;
public class Klient5_AsyncHandler {
public static void main(String[] args) {
BigDecimal min = BigDecimal.valueOf(2000);
BigDecimal max = BigDecimal.valueOf(3000);
System.out.println("Początek programu");
SklepService service = new SklepService();
Sklep sklep = service.getSklepPort();
System.out.println("Klient gotowy do pracy");
System.out.println("Wysyłam zapytanie");
// jako ostatni parametr przekazujemy AsyncHandler określając, co zrobić po otrzymaniu wyniku
// Od Javy 8 najłatwiej przekazać to w formie wyrażenia lambda
// ten kod wykona się w innym wątku po zakończeniu zadania
Future<?> future = sklep.readProductsByPriceAsync(min, max, response -> {
try {
List<Product> products = response.get().getProduct();
for(Product product : products) {
System.out.println(product.getProductName() + " za " + product.getPrice());
}
} catch(InterruptedException e) {
System.out.println("interrupted");
} catch(ExecutionException e) {
System.out.println("Błąd w wykonaniu: " + e);
}
});
for(int i = 1; i <= 50; i++) {
System.out.println("lalala, robię coś inngo");
}
System.out.println("teraz poczekam na koniec zlecenia");
try {
future.get();
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
} catch(ExecutionException e) {
e.printStackTrace();
}
System.out.println("Koniec main");
}
}
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