简体   繁体   English

如何重用Web服务的实例?

[英]How can I reuse an instance of a webservice?

I am running a service A which has a class X . 我运行的服务A的类别为X。 I am going to deploy another service B on same machine which is using this class X . 我将在使用此类X的同一台计算机上部署另一个服务B。 How can I make sure that the same instance of service A is reused instead of another. 如何确保服务A的同一实例被重用,而不是另一个。

PS:Service written in JAVA. PS:用JAVA编写的服务。

Adding: Both these services are Axis2 services. 添加:这两个服务都是Axis2服务。 Service B is hot-deployed. 服务B已热部署。 Service B used class Y which is extension of class X. 服务B使用了Y类,它是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: 选择1:

Directly call A from B. You are asking how to do that. 直接从B呼叫A。您正在询问该怎么做。 It could be done. 可以做到的。 You would just code a JAX-WS client call in your implmentation. 您只需在实现中编写一个JAX-WS客户端调用的代码即可。 However I stringly recommend against this. 但是,我强烈建议不要这样做。 A service call is likely to be more expensive than a simple Java call. 服务调用可能比简单的Java调用更昂贵。

Only do this if y6ou don't have the option of deploying the two service classes together. 仅当y6ou无法选择将两个服务类一起部署时,才执行此操作。

Alternative 2: 选择2:

Refactor the implementation. 重构实现。 Just move the code into a worker class. 只需将代码移到worker类中即可。

@javax.jws.WebService public class ServiceAAA{ @ javax.jws.WebService公共类ServiceAAA {

    MyWorker worker = new Worker();
    public String echo(String arg) {
        return worker.doSomething(arg) ;      
    }

}

@javax.jws.WebService public class ServiceBBB{ @ javax.jws.WebService公共类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. 我知道A也会使用X类,而B也会使用。

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: 将两个Web服务A和B配置为使用对象X的相同实例。可以通过多种方式来完成此配置,例如:

  • in your starting sequence, create an instance X and assign it via setX(x) to each webService. 按照开始的顺序,创建一个实例X,然后通过setX(x)将其分配给每个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
       }
     }

Don't know java, but you could make use of a singleton pattern on the objects your are trying to use. 不懂Java,但是您可以在尝试使用的对象上使用单例模式

edit: I think you should have class X implementing the singleton pattern... 编辑:我认为您应该让X类实现单例模式...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM