Java Web服务中的Singleton对象 [英] Singleton Object in Java Web service

查看:88
本文介绍了Java Web服务中的Singleton对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,
我目前正在开发一个公开Web服务接口的java Web应用程序。为了将全局对象保留在内存中,我将以下类用作Singleton:

Good morning, I am currently developing a java web application that exposes a web service interface. In order to keep a global object in memory, I use the following class as a Singleton:

public class SingletonMap {
    private static final SingletonMap instance = new SingletonMap();
    private static HashMap couponMap = null;
    private static long creationTime;

    private SingletonMap() {
        creationTime = System.currentTimeMillis();
        couponMap = new HashMap();
    }

    public static synchronized SingletonMap getInstance() {
        return instance;
    }

    public static long getCreationTime() {
        return creationTime;
    }
}

我使用上面的类来获得Web服务的所有线程的HashMap的相同实例。维护SingletonMap对象的Web服务类如下:

I am using the above class in order to have the same instance of the HashMap for all the threads of the web service. The Web service class that maintains the SingletonMap object is the following:

@WebService()
public class ETL_WS {
    private String TOMCAT_TEMP_DIR;
    private final int BUFFER_SIZE = 10000000;
    private static SingletonMap couponMap;
    private static SingletonProductMap productCategoryMap;
    private String dbTable = "user_preferences";

    public ETL_WS() {
        Context context = null;
        try {
            context = (Context) new InitialContext().lookup("java:comp/env");
            this.TOMCAT_TEMP_DIR = (String) context.lookup("FILE_UPLOAD_TEMP_DIR");
        }catch(NamingException e) {
        System.err.println(e.getMessage());
    }

    public long getCouponMapCreationTime() {
        return couponMap.getCreationTime();
    }

}

我有方法getCouponMapCreationTime的原因()是检查Web服务的所有线程是否正在访问同一个对象。上述方法是否正确?性能开销怎么样?你认为我需要Singleton属性,还是我可以只为所有线程使用静态HashMap?如果我使用静态HashMap,如果没有线程处于活动状态,它是否会被垃圾收集?

The reason i have the method getCouponMapCreationTime() is to check that all the threads of the web service are accessing the same object. Is the above approach correct? How about performance overheads? Do you think I need the Singleton properties, or could I just use a static HashMap for all the threads? If I use a static HashMap, is it going to be garbage collected in case no thread is active?

感谢您的时间。

推荐答案

JAX-WS Web服务本身就是一个Singleton。这意味着将使用单个Web服务实例(如Servlet)处理所有请求。

A JAX-WS web service is by itself a Singleton. This means that all the request will be handled using a single web service instance (like a Servlet).

因此,类中的任何成员都将在所有成员之间共享请求。在您的情况下,您不需要使您的成员(即couponMap)成为静态属性。

So, any member of the class will be 'shared' between all the request. In your case, you do not need to make your members (i.e. couponMap) an static attributes.

结论:别担心,所有你的线程(请求)将访问相同的'couponMap'。因为你不再需要 getCouponMapCreationTime ,我认为你可以消除 SingletonMap 抽象并直接使用Map在您的Web服务类中。

Conclusion: Don't worry, all your threads (request) will be accessing the same 'couponMap'. Because you don't need the getCouponMapCreationTime anymore, I think that you can eliminate the SingletonMap abstraction and use directly a Map in your web service class.

但我有一些非常重要的事情要添加。如果几个线程(请求)将访问您的地图,您必须使其线程安全!有很多方法可以做到这一点,但我会提出一个想法:使用 ConcurrentHashMap 而不是 HashMap 。这将使你所有的 get(),put(),remove()操作都是线程安全的!如果您需要更大的范围,则可以使用synchronized块,但请避免使用同步方法,因为scoop太大并且始终在对象上进行同步。

But I have something very important to add. If several threads (request) will be accessing your Map you have to make it thread-safe!!! There are a lot of way to do this, but I will give an idea: Use a ConcurrentHashMap instead of a HashMap. This will make all your get(), put(), remove() operations thread-safe! If you need a larger scope you can use synchronized blocks, but please avoid synchronize methods because the scoop is too large and always synchronize over this object.

这篇关于Java Web服务中的Singleton对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆