The URL of the service is set through the serviceUrl property. Here, the handler is named BTTRemoteConnector and is hosted on a machine whose name is configured using a property placeholder. The tag name specifies the interface that the service implements and through which the client invokes methods on the service. serviceId specified the remote service name that defined in server side.
After finish the configuration in client side, you can call the service in the client side.
Here is an example of calling the service in the client side.
ElementFactory factory = new BasicElementFactory("jar:///client.xml"); TestServiceInterface serviceProxy = (TestServiceInterface) factory.getElement("localService");
The interaction between the client and the proxy is illustrated as follow:
Prerequisite: To export the service in server side, you should do the following steps:
1 Create the proxy:
▪ Use a interface on the client side. Here is an example:
public interface TestServiceInterface { public String getYear(String name); public void setYear(String name, String year) throws TestException; }
▪ Implement the service interface on the server side. Here is an example:
public class TestService implements TestServiceInterface { private Map< String, String> values = new HashMap< String, String>(); public String getYear(String name) { return values.get(name); } public void setYear(String name, String year) throws TestException { System.out.println("name: " + name + " age: " + year); if (name.equals(year)) { throw new TestException("name: " + name + " age: " + year); } values.put(name, year); } }
2 Configure the web container, which is the second level container on the server side. To enable Remote on the server side, you should do the following steps:
▪ Add some parameters and a listener for the service connector. For example:
The elementFactoryConfigPath parameter indicates the path of the UDTT configuration files. The BTTServerStarter, which is provided by Remote, is a listener which can be notified when web server started
▪ Add a servlet, which serves as the handler for the remote invocation. For example:
Scope management of Remote Connection for POJO service
The POJO service is instantiated by UDTT Element Factory on the server side. And the instance lifecycle can be specified by the configuration of the UDTT Element Factory, using a attribute named scope. UDTT Element Factory provides the following three options for the scope attribute:
▪ singleton. It means that there is only one service instance in the server environment and only one instance for one particular id.
▪ prototype. It means when the server receives a client request, one instance of the service is instantiated. And the instance is destroyed after the execution of the method.
▪ session. It means the lifecycle of the instance is the same as the session in the web container, which seems shorter than the singleton but longer than the prototype.
If the scope of one service is not set, the default value will follow the global one.
The init method of the service instance will be invoked to execute some initialization actions after the object is created. And the destroy method of the service instance will be invoked before the object is destroyed.
The prototype means when the server receives a client request, one instance of the service is instantiated. And the instance is destroyed after the execution of the method.
The process of setting up a Prototype Remote connection is the same as the Singleton Remote Connection, except for the service registration on the server side. The scope is set to prototype. For example, <service.TestService id="testService" scope=”prototype”/>
The session means the lifecycle of the instance is the same as the session in the web container, which seems shorter than the singleton but longer than the prototype.
The process is basic the same with Singleton Remote Connection, except two difference.
1 Registry the service in the Element Factory configuration file on the server side, set the scope to session. Here is an example: