|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.cache;
|
18 | 18 |
|
| 19 | +import java.io.Closeable; |
19 | 20 | import java.io.IOException;
|
20 | 21 |
|
| 22 | +import com.hazelcast.core.Hazelcast; |
| 23 | +import com.hazelcast.core.HazelcastInstance; |
| 24 | +import com.hazelcast.spring.cache.HazelcastCacheManager; |
| 25 | + |
21 | 26 | import org.springframework.beans.factory.annotation.Autowired;
|
22 |
| -import org.springframework.boot.autoconfigure.condition.ConditionOutcome; |
| 27 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
23 | 28 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
24 | 29 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; |
| 31 | +import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; |
| 32 | +import org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition; |
25 | 33 | import org.springframework.cache.CacheManager;
|
26 | 34 | import org.springframework.context.annotation.Bean;
|
27 |
| -import org.springframework.context.annotation.ConditionContext; |
28 | 35 | import org.springframework.context.annotation.Conditional;
|
29 | 36 | import org.springframework.context.annotation.Configuration;
|
30 | 37 | import org.springframework.core.io.Resource;
|
31 |
| -import org.springframework.core.type.AnnotatedTypeMetadata; |
32 |
| - |
33 |
| -import com.hazelcast.config.Config; |
34 |
| -import com.hazelcast.config.XmlConfigBuilder; |
35 |
| -import com.hazelcast.core.Hazelcast; |
36 |
| -import com.hazelcast.core.HazelcastInstance; |
37 |
| -import com.hazelcast.spring.cache.HazelcastCacheManager; |
38 | 38 |
|
39 | 39 | /**
|
40 |
| - * Hazelcast cache configuration. Only kick in if a configuration file location is set or |
41 |
| - * if a default configuration file exists (either placed in the default location or set |
42 |
| - * via the {@value #CONFIG_SYSTEM_PROPERTY} system property). |
| 40 | + * Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that |
| 41 | + * has been configured by the general {@link HazelcastAutoConfiguration} or create |
| 42 | + * a separate one if the {@code spring.cache.hazelcast.config} property has been set. |
| 43 | + * <p> |
| 44 | + * If the {@link HazelcastAutoConfiguration} has been disabled, an attempt to configure |
| 45 | + * a default {@link HazelcastInstance} is still made, using the same defaults. |
43 | 46 | *
|
44 | 47 | * @author Stephane Nicoll
|
45 | 48 | * @since 1.3.0
|
| 49 | + * @see HazelcastConfigResourceCondition |
46 | 50 | */
|
47 | 51 | @Configuration
|
48 |
| -@ConditionalOnClass({ HazelcastInstance.class, HazelcastCacheManager.class }) |
| 52 | +@ConditionalOnClass({HazelcastInstance.class, HazelcastCacheManager.class}) |
49 | 53 | @ConditionalOnMissingBean(CacheManager.class)
|
50 |
| -@Conditional({ CacheCondition.class, |
51 |
| - HazelcastCacheConfiguration.ConfigAvailableCondition.class }) |
| 54 | +@Conditional(CacheCondition.class) |
| 55 | +@AutoConfigureAfter(HazelcastAutoConfiguration.class) |
52 | 56 | class HazelcastCacheConfiguration {
|
53 | 57 |
|
54 |
| - static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.config"; |
| 58 | + @Configuration |
| 59 | + @ConditionalOnSingleCandidate(HazelcastInstance.class) |
| 60 | + static class ExistingHazelcastInstanceConfiguration { |
| 61 | + |
| 62 | + @Autowired |
| 63 | + private CacheProperties cacheProperties; |
55 | 64 |
|
56 |
| - @Autowired |
57 |
| - private CacheProperties cacheProperties; |
| 65 | + @Bean |
| 66 | + public HazelcastCacheManager cacheManager(HazelcastInstance existingHazelcastInstance) |
| 67 | + throws IOException { |
| 68 | + Resource location = this.cacheProperties |
| 69 | + .resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig()); |
| 70 | + if (location != null) { |
| 71 | + HazelcastInstance cacheHazelcastInstance = |
| 72 | + HazelcastAutoConfiguration.createHazelcastInstance(location); |
| 73 | + return new CloseableHazelcastCacheManager(cacheHazelcastInstance); |
| 74 | + } |
| 75 | + else { |
| 76 | + return new HazelcastCacheManager(existingHazelcastInstance); |
| 77 | + } |
58 | 78 |
|
59 |
| - @Bean |
60 |
| - public HazelcastCacheManager cacheManager(HazelcastInstance hazelcastInstance) { |
61 |
| - return new HazelcastCacheManager(hazelcastInstance); |
| 79 | + } |
62 | 80 | }
|
63 | 81 |
|
64 |
| - @Bean |
65 |
| - @ConditionalOnMissingBean |
66 |
| - public HazelcastInstance hazelcastInstance() throws IOException { |
67 |
| - Resource location = this.cacheProperties |
68 |
| - .resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig()); |
69 |
| - if (location != null) { |
70 |
| - Config cfg = new XmlConfigBuilder(location.getURL()).build(); |
71 |
| - return Hazelcast.newHazelcastInstance(cfg); |
| 82 | + @Configuration |
| 83 | + @ConditionalOnMissingBean(HazelcastInstance.class) |
| 84 | + @Conditional(ConfigAvailableCondition.class) |
| 85 | + static class DefaultHazelcastInstanceConfiguration { |
| 86 | + |
| 87 | + @Autowired |
| 88 | + private CacheProperties cacheProperties; |
| 89 | + |
| 90 | + @Bean |
| 91 | + public HazelcastInstance hazelcastInstance() throws IOException { |
| 92 | + Resource location = this.cacheProperties |
| 93 | + .resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig()); |
| 94 | + if (location != null) { |
| 95 | + HazelcastAutoConfiguration.createHazelcastInstance(location); |
| 96 | + } |
| 97 | + return Hazelcast.newHazelcastInstance(); |
| 98 | + } |
| 99 | + |
| 100 | + @Bean |
| 101 | + public HazelcastCacheManager cacheManager() throws IOException { |
| 102 | + return new HazelcastCacheManager(hazelcastInstance()); |
72 | 103 | }
|
73 |
| - return Hazelcast.newHazelcastInstance(); |
| 104 | + |
74 | 105 | }
|
75 | 106 |
|
76 | 107 | /**
|
77 |
| - * Determines if the Hazelcast configuration is available. This either kicks in if a |
78 |
| - * default configuration has been found or if property referring to the file to use |
79 |
| - * has been set. |
| 108 | + * {@link HazelcastConfigResourceCondition} that checks if the |
| 109 | + * {@code spring.cache.hazelcast.config} configuration key is defined. |
80 | 110 | */
|
81 |
| - static class ConfigAvailableCondition extends CacheConfigFileCondition { |
| 111 | + static class ConfigAvailableCondition extends HazelcastConfigResourceCondition { |
82 | 112 |
|
83 | 113 | public ConfigAvailableCondition() {
|
84 |
| - super("Hazelcast", "spring.cache.hazelcast", "file:./hazelcast.xml", |
85 |
| - "classpath:/hazelcast.xml"); |
| 114 | + super("spring.cache.hazelcast", "config"); |
86 | 115 | }
|
87 | 116 |
|
88 |
| - @Override |
89 |
| - protected ConditionOutcome getResourceOutcome(ConditionContext context, |
90 |
| - AnnotatedTypeMetadata metadata) { |
91 |
| - if (System.getProperty(CONFIG_SYSTEM_PROPERTY) != null) { |
92 |
| - return ConditionOutcome.match("System property '" |
93 |
| - + CONFIG_SYSTEM_PROPERTY + "' is set."); |
94 |
| - } |
95 |
| - return super.getResourceOutcome(context, metadata); |
| 117 | + } |
| 118 | + |
| 119 | + private static class CloseableHazelcastCacheManager extends HazelcastCacheManager implements Closeable { |
| 120 | + private final HazelcastInstance hazelcastInstance; |
| 121 | + |
| 122 | + public CloseableHazelcastCacheManager(HazelcastInstance hazelcastInstance) { |
| 123 | + super(hazelcastInstance); |
| 124 | + this.hazelcastInstance = hazelcastInstance; |
96 | 125 | }
|
97 | 126 |
|
| 127 | + @Override |
| 128 | + public void close() throws IOException { |
| 129 | + this.hazelcastInstance.shutdown(); |
| 130 | + } |
98 | 131 | }
|
99 | 132 |
|
100 | 133 | }
|
0 commit comments