RookieDev
RookieDev

Reputation: 261

Starting with Hazelcast and Spring Boot


I have just started with hazelcast and am trying to do a POC with spring boot 1.5.8.

What i am trying to do to start with is to create a spring boot application in which i have created a Config bean and since i have hazelcast jar on the classpath, spring does create a hazelcast instance for me, which i have used and created an IMap and put in some test values in the map

  1. From my understanding, i think i would need a hazelcast client to get access to the distribute map.
  2. So, what i did was, I created a bean of type ClientConfig and i expect spring to give me a "HazelcastInstance" of type HazelcastClient, but i dont think i am getting that.

Any pointers, on how to run hazelcast members and client in the same application?

.Thanks

,

Upvotes: 0

Views: 1620

Answers (1)

Neil Stevenson
Neil Stevenson

Reputation: 3150

Try

@Bean
public ClientConfig clientConfig() throws Exception {
        return new XmlClientConfigBuilder("hazelcast-client.xml").build();
}
@Bean
public Config config() {
    return new ClasspathXmlConfig("hazelcast.xml");
}
@Bean(name="server")
public HazelcastInstance server(Config config) {
    return Hazelcast.newHazelcastInstance(config);
}
@Bean(name="client")
@DependsOn("server")
public HazelcastInstance client(ClientConfig clientConfig) {
    return HazelcastClient.newHazelcastClient(clientConfig);
}

and access with

@Autowired
@Qualifier("server")
private HazelcastInstance server;
@Autowired
@Qualifier("client")
private HazelcastInstance client;

You need @Depends on so the server is up before the client.

Upvotes: 1

Related Questions