diff options
author | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-04-17 15:22:18 +0200 |
---|---|---|
committer | Christof Rabensteiner <christof.rabensteiner@iaik.tugraz.at> | 2019-04-17 15:22:18 +0200 |
commit | 8e362d5feaf5535bb59bacc1be677d8f10d08f86 (patch) | |
tree | e37505498ad18361df3fa3de973b094dced59f61 | |
parent | ef9a86858a5e05b4dd9bff3992c477f49b0b20f2 (diff) | |
download | moa-zs-8e362d5feaf5535bb59bacc1be677d8f10d08f86.tar.gz moa-zs-8e362d5feaf5535bb59bacc1be677d8f10d08f86.tar.bz2 moa-zs-8e362d5feaf5535bb59bacc1be677d8f10d08f86.zip |
Add Redis and InMemory Implementation of DeliveryRequestRepository
- Deployer can choose which implementation to use (by activating
- corresponding profile)
- Add guava dependency
- Extract DeliveryRequestRepository to Interface
- Implement InMemory Repository using guava's cache
-rw-r--r-- | pom.xml | 7 | ||||
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/DeliveryRepository.java | 15 | ||||
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/DeliveryRequestHandler.java | 3 | ||||
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/InMemoryDeliveryRepository.java | 40 | ||||
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/RedisConfig.java | 10 | ||||
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/RedisDeliveryRepository.java (renamed from src/main/java/at/gv/egiz/moazs/RedisRepository.java) | 26 | ||||
-rw-r--r-- | src/main/resources/application.properties | 2 |
7 files changed, 80 insertions, 23 deletions
@@ -25,6 +25,7 @@ <jaxwsapi.version>2.3.1</jaxwsapi.version> <jwsapi.version>1.1</jwsapi.version> <jedisclient.version>2.9.3</jedisclient.version> + <guava.version>27.1-jre</guava.version> </properties> <dependencies> @@ -63,6 +64,12 @@ <artifactId>jedis</artifactId> <version>${jedisclient.version}</version> </dependency> + <!-- guava --> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <version>${guava.version}</version> + </dependency> <!-- needed by cxf-codegen-plugin's generated code --> <dependency> <groupId>javax.xml.ws</groupId> diff --git a/src/main/java/at/gv/egiz/moazs/DeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/DeliveryRepository.java new file mode 100644 index 0000000..6df2829 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/DeliveryRepository.java @@ -0,0 +1,15 @@ +package at.gv.egiz.moazs; + +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface DeliveryRepository { + + void add(DeliveryRequestType request); + + Optional<DeliveryRequestType> getDeliveryRequest(String appDeliveryID); + +} diff --git a/src/main/java/at/gv/egiz/moazs/DeliveryRequestHandler.java b/src/main/java/at/gv/egiz/moazs/DeliveryRequestHandler.java index 16235cf..7d8cb43 100644 --- a/src/main/java/at/gv/egiz/moazs/DeliveryRequestHandler.java +++ b/src/main/java/at/gv/egiz/moazs/DeliveryRequestHandler.java @@ -7,7 +7,6 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import javax.xml.bind.JAXBElement; import java.util.function.Function; @Component @@ -16,7 +15,7 @@ public class DeliveryRequestHandler implements Function<DeliveryRequestType, Del private static final Logger logger = LoggerFactory.getLogger(DeliveryRequestHandler.class); @Autowired - private RedisRepository repository; + private DeliveryRepository repository; /** * Handles the Delivery Request diff --git a/src/main/java/at/gv/egiz/moazs/InMemoryDeliveryRepository.java b/src/main/java/at/gv/egiz/moazs/InMemoryDeliveryRepository.java new file mode 100644 index 0000000..aa1e3e9 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/InMemoryDeliveryRepository.java @@ -0,0 +1,40 @@ +package at.gv.egiz.moazs; + +import at.gv.e_government.reference.namespace.zustellung.mzs.app2mzs_.DeliveryRequestType; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Repository; + +import java.util.Optional; +import java.util.concurrent.TimeUnit; + +@Repository +@Profile("!cluster") +public class InMemoryDeliveryRepository implements DeliveryRepository { + + private static final Logger logger = LoggerFactory.getLogger(InMemoryDeliveryRepository.class); + + private static final Cache<String, DeliveryRequestType> rep = CacheBuilder.newBuilder() + .expireAfterWrite(30, TimeUnit.MINUTES) + .build(); + + public InMemoryDeliveryRepository() { + logger.info("InMemoryDeliveryRepository()"); + } + + @Override + public void add(DeliveryRequestType request) { + String key = request.getMetaData().getAppDeliveryID(); + logger.info("Save DeliveryRequest with AppDeliveryId = {} to Cache.", key); + rep.put(key, request); + } + + @Override + public Optional<DeliveryRequestType> getDeliveryRequest(String appDeliveryID) { + DeliveryRequestType deliveryRequestType = rep.getIfPresent(appDeliveryID); + return Optional.ofNullable(deliveryRequestType); + } +} diff --git a/src/main/java/at/gv/egiz/moazs/RedisConfig.java b/src/main/java/at/gv/egiz/moazs/RedisConfig.java index 758fbfd..321ec4a 100644 --- a/src/main/java/at/gv/egiz/moazs/RedisConfig.java +++ b/src/main/java/at/gv/egiz/moazs/RedisConfig.java @@ -3,20 +3,17 @@ package at.gv.egiz.moazs; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.serializer.GenericToStringSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; -import org.springframework.oxm.jaxb.Jaxb2Marshaller; - -import java.util.HashMap; @Configuration -@PropertySource("classpath:application.properties") public class RedisConfig { + public RedisConfig() { + } + @Value("${spring.redis.host}") private String host; @@ -43,5 +40,4 @@ public class RedisConfig { template.setDefaultSerializer(stringRedisSerializer()); return template; } - } diff --git a/src/main/java/at/gv/egiz/moazs/RedisRepository.java b/src/main/java/at/gv/egiz/moazs/RedisDeliveryRepository.java index db49b23..accae43 100644 --- a/src/main/java/at/gv/egiz/moazs/RedisRepository.java +++ b/src/main/java/at/gv/egiz/moazs/RedisDeliveryRepository.java @@ -6,48 +6,46 @@ import org.apache.tools.ant.filters.StringInputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Repository; -import javax.annotation.PostConstruct; import javax.xml.bind.JAXBElement; import java.util.Optional; @Repository -public class RedisRepository { +@Profile("cluster") +public class RedisDeliveryRepository implements DeliveryRepository { - private static final Logger logger = LoggerFactory.getLogger(RedisRepository.class); + private static final Logger logger = LoggerFactory.getLogger(RedisDeliveryRepository.class); private static final String KEY = "MZS_DELIVERY_REQUEST"; private final RedisTemplate<String, Object> redisTemplate; - private final Marshaller marshaller; - - private HashOperations<String, String, String> hashOperations; - + private final HashOperations<String, String, String> hashOperations; private final ObjectFactory factory = new ObjectFactory(); @Autowired - public RedisRepository(RedisTemplate<String, Object> redisTemplate, Marshaller marshaller){ + public RedisDeliveryRepository(RedisTemplate<String, Object> redisTemplate, Marshaller marshaller){ + logger.info("RedisDeliveryRepository()"); + this.redisTemplate = redisTemplate; + this.hashOperations = redisTemplate.opsForHash(); this.marshaller = marshaller; } - @PostConstruct - private void init(){ - hashOperations = redisTemplate.opsForHash(); - } - + @Override public void add(DeliveryRequestType request) { var hashKey = request.getMetaData().getAppDeliveryID(); var serializedRequest = marshaller.marshallXml(factory.createDeliveryRequest(request)); - logger.info("Save DeliveryRequest with AppDeliveryId = {}", hashKey); + logger.info("Save DeliveryRequest with AppDeliveryId = {} to redis.", hashKey); hashOperations.put(KEY, hashKey, serializedRequest); } + @Override public Optional<DeliveryRequestType> getDeliveryRequest(String appDeliveryID) { var serializedRequest = (String) hashOperations.get(KEY, appDeliveryID); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7cf70bd..7bd66e2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,4 @@ spring.redis.host=172.17.0.2 spring.redis.port=6379 +## activate cluster mode +# spring.profiles.active=cluster |