views:

71

answers:

3

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.

A: 

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:

  • in your starting sequence, create an instance X and assign it via setX(x) to each webService.
  • or you could do this in a Constructor, using static fields

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
       }
     }
KLE
Its 2 - service A uses object of class X.What do you mean by starting sequence? Both these services are Axis2 services. Service B is hot-deployed. Service B used class Y which is extension of class X.
AJ
@Ankit "starting sequence" is a code that you run when your application is starting. Very often, you have a place like this, where you execute the code that is needed only once ...
KLE
@Ankit If Y (used by B) is a subclass of X (used by A), how could they be the same instance? I don't understand...
KLE
@KLE Y uses an instance of X. I want it to reuse the existing instance.
AJ
+1  A: 

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             
    }    
}
djna
Thank you for replying! I want to call A from B. Objective here is to use the same code base and infrastructure i.e - Reuse already running instance.
AJ
OK, I understand now. I'll amend my answer slightly.
djna
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...

deostroll