如何在连接到hazelcast客户端期间处理异常

时间:2017-10-04 10:08:49

标签: java hazelcast

所以我使用spring和hazelcast客户端来执行一些任务。有没有办法处理IlllegalStateException:

Caused by: java.lang.IllegalStateException: Unable to connect to any address in the config! The following addresses were tried: []
        at com.hazelcast.client.spi.impl.ClusterListenerSupport.connectToCluster(ClusterListenerSupport.java:178) ~[hazelcast-client-3.8.6.jar:3.8.6]
        at com.hazelcast.client.spi.impl.ClientClusterServiceImpl.start(ClientClusterServiceImpl.java:189) ~[hazelcast-client-3.8.6.jar:3.8.6]
        at com.hazelcast.client.impl.HazelcastClientInstanceImpl.start(HazelcastClientInstanceImpl.java:404) ~[hazelcast-client-3.8.6.jar:3.8.6]
        at com.hazelcast.client.HazelcastClientManager.newHazelcastClient(HazelcastClientManager.java:78) ~[hazelcast-client-3.8.6.jar:3.8.6]
        at com.hazelcast.client.HazelcastClient.newHazelcastClient(HazelcastClient.java:72) ~[hazelcast-client-3.8.6.jar:3.8.6]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_74]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_74]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_74]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_74]

在xml中我有:

<bean id="hazelcast" class="com.hazelcast.client.HazelcastClient" factory-method="newHazelcastClient">
    <constructor-arg>
        <bean class="com.hazelcast.client.config.ClientConfig">
            <property name="networkConfig" ref="hazelcastNetworkConfig"/>
        </bean>
    </constructor-arg>
</bean>

并在代码中我自动连接此客户端。

@Autowired
private HazelcastInstance hazelcast;

当连接出现问题时,我甚至无法部署我的应用程序。因此,例如当连接实例存在问题时,应该为null,我将使用我的缓存或一些不同的解决方案。我不想在无法使用淡褐色时抛出异常

        if (useHazelCast(...))
            useHazelcastCache(...)
        else
            useLocalCache(...);

1 个答案:

答案 0 :(得分:0)

不是Spring专家,但我会继续使用我自己的提供程序,它根本不会创建Hazelcast实例,但在localCache = true的情况下返回null。