I am running a service A which has a class X . I am going to deploy another service B on same machine which is using this class X . How can I make sure that the same instance of service A is reused instead of another.
PS:Service written in JAVA.
Adding: Both these services are Axis2 services. Service B is hot-deployed. Service B used class Y which is extension of class X.
Could we try to distinguish classes, objects and services.
You have something like this?
@javax.jws.WebService
public class ServiceAAA{
public String echo(String arg) {
// some really nice code here
}
}
and you want to add
@javax.jws.WebService
public class ServiceBBB{
public String superEcho(String arg) {
// even more code here
// which needs to reuse the code from A's echo()
}
}
So clearly we don't want to cut and paste between the two implementations. How do we reuse?
Alternative 1:
Directly call A from B. You are asking how to do that. It could be done. You would just code a JAX-WS client call in your implmentation. However I stringly recommend against this. A service call is likely to be more expensive than a simple Java call.
Only do this if y6ou don't have the option of deploying the two service classes together.
Alternative 2:
Refactor the implementation. Just move the code into a worker class.
@javax.jws.WebService public class ServiceAAA{
MyWorker worker = new Worker();
public String echo(String arg) {
return worker.doSomething(arg) ;
}
}
@javax.jws.WebService public class ServiceBBB{
MyWorker worker = new Worker();
public String superEcho(String arg) {
worker.doSomething(arg) ;
// and some morestuff
}
}
I understand that A uses an object of class X, and B too.
Configure your two webServices A and B to use the same instance of object X. This configuration could be done by several means, for example:
setX(x)
to each webService. Example:
@javax.jws.WebService
public class A implements WebService {
public static final X x = new X();
public void methodA() {
// use x
}
}
@javax.jws.WebService
public class B implements WebService {
private Y y = new Y(A.x);
public void methodB() {
// use y that uses x.
y.methodY();
}
}
public class Y {
private final X x;
public Y(X x) {
this.x = x;
}
public void methodY() {
// use x, it is the same instance as in A
}
}
Don't know java, but you could make use of a singleton pattern on the objects your are trying to use.
edit: I think you should have class X implementing the singleton pattern...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.