一个简单的例子让我们看看被一个客户端使用的一个异步服务的简单例子——在这个例子中,客户端和服务都使用普通的Java类编写。这是一个对某些物品下订单的服务的简单版本,其中服务是异步的,通过一个回调接口通知客户端进度信息。
首先,服务和回调接口如下:
public interface OrderService {
public void placeOrder( OrderRequest oRequest );
}
例 7:OrderService接口public interface OrderCallback {
public void placeOrderResponse( OrderResponse oResponse );
}
例 8:OrderCallback接口接下来是OrderService(即服务提供者)的实现代码:
import org.osoa.sca.annotations.*;
public class OrderServiceImpl implements OrderService {
// A field for the callback reference object
private OrderCallback callbackReference;
// The place order operation itself
public void placeOrder( OrderRequest oRequest ) {
// …do the work to process the order…
// …which may take some time…
// when ready to respond…
OrderResponse theResponse = new OrderResponse();
callbackReference.placeOrderResponse( theResponse );
}
// A setter method for the callback reference
@Callback
public void setCallbackReference( OrderCallback theCallback ) {
callbackReference = theCallback;
}
}
例 9:OrderService实现最后是OrderService客户端的代码,它提供了OrderCallback接口的实现:
import org.osoa.sca.annotations.*;
public class OrderServiceClient implements OrderCallback {
// A field to hold the reference to the order service
private OrderService orderService;
public void doSomeOrdering() {
OrderRequest oRequest = new OrderRequest();
//… fill in the details of the order …
orderService.placeOrder( oRequest );
// …the client code can continue to do processing
}
public void placeOrderResponse( OrderResponse oResponse ) {
// …handle the response as needed
}
// A setter method for the order service reference
@Reference
public void setOrderService( OrderService theService ) {
orderService = theService;
}
}
例 10:OrderService的客户端