线程使用的System.setProperty会影响与外部网络元素通信的其他线程。怎么解决? [英] System.setProperty used by a thread impacts other thread in communication to external network elements. How to resolve it?

查看:96
本文介绍了线程使用的System.setProperty会影响与外部网络元素通信的其他线程。怎么解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有两个主题。每个线程与不同的外部实体进行通信。

In my application, I have two threads. Each thread communicates to different external entities.

让我们说T1 - > N1& T2 - > N2(T1和T2是两个线程.N1和N2是外部实体。通信是基于HTTPS的SOAP。)

Let us say T1 --> N1 & T2 --> N2 (T1 & T2 are two threads. N1 & N2 are external entities. Communications is SOAP over HTTPS.)

N1的供应商要求使用密钥库文件 UPCC_client.store 进行身份验证,同样我们使用了以下代码,

The vendor of N1 requested to use key store file UPCC_client.store for authentication and for the same we have used the following code,

System.setProperty("javax.net.ssl.keyStore", "<file path>");
System.setProperty("javax.net.ssl.keyStorePassword", "<password>");
System.setProperty("javax.net.ssl.trustStore","<file path>");
System.setProperty("javax.net.ssl.trustStorePassword", "<password>");

应用程序已重新启动,并且在T1线程中设置了上述属性,没有任何问题。 T2开始陷入困境,因为T1设置的属性正被T2使用。这背后的主要原因是 System.setProperty 是JVM范围。如何解决这个问题?

The application has been restarted with the above properties set in T1 thread with no issues. T2 started getting into trouble, since properties set by T1 are getting used by T2. The main reason behind this is System.setProperty is JVM scope. How to resolve this problem?

推荐答案

我怀疑你有一个设计问题,但是有这个要求。

I suspect you have a design issue to have this requirement however.

唯一能解决这个问题的方法就是让你的属性成为ThreadLocal。

The only way around this is I can think of is to make your properties ThreadLocal.

public class ThreadLocalProperties extends Properties {
    private final ThreadLocal<Properties> localProperties = new ThreadLocal<Properties>() {
        @Override
        protected Properties initialValue() {
            return new Properties();
        }
    };

    public ThreadLocalProperties(Properties properties) {
        super(properties);
    }

    @Override
    public String getProperty(String key) {
        String localValue = localProperties.get().getProperty(key);
        return localValue == null ? super.getProperty(key) : localValue;
    }

    @Override
    public Object setProperty(String key, String value) {
        return localProperties.get().setProperty(key, value);
    }
}

// Make the properties thread local from here. This to be done globally once.
System.setProperties(new ThreadLocalProperties(System.getProperties()));

// in each thread.
System.setProperty("javax.net.ssl.keyStore", "my-key-store");






除非有任何混淆,否则System.setProperties( )不只是设置属性,它取代了集合及其实现。


Unless there is any confusion, System.setProperties() doesn't just set properties, it replaces the collection, and its implementation.

// From java.lang.System
 * The argument becomes the current set of system properties for use
 * by the {@link #getProperty(String)} method.

public static void setProperties(Properties props) {
    SecurityManager sm = getSecurityManager();
    if (sm != null) {
        sm.checkPropertiesAccess();
    }
    if (props == null) {
        props = new Properties();
        initProperties(props);
    }
    System.props = props;
}

通过使用此方法,系统属性的行为更改为调用setProperty()和getProperty()

By using this method the behaviour of System Properties changes to being thread local for calls to setProperty() and getProperty()

这篇关于线程使用的System.setProperty会影响与外部网络元素通信的其他线程。怎么解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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