implemeneted redis support

This commit is contained in:
strawmanbobi
2018-12-29 18:19:07 +08:00
parent fdf10dca1b
commit 6de67d6ade
12 changed files with 279 additions and 4 deletions

4
.idea/compiler.xml generated
View File

@@ -6,11 +6,11 @@
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="decode-web-service" />
<module name="decode-service" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="decode-web-service" target="1.8" />
<module name="decode-service" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>

2
.idea/modules.xml generated
View File

@@ -2,7 +2,7 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/decode-web-service.iml" filepath="$PROJECT_DIR$/decode-web-service.iml" />
<module fileurl="file://$PROJECT_DIR$/decode-service.iml" filepath="$PROJECT_DIR$/decode-service.iml" />
</modules>
</component>
</project>

1
.idea/vcs.xml generated
View File

@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -0,0 +1,65 @@
package net.irext.decoder;
import net.irext.decoder.queue.MessagePublisher;
import net.irext.decoder.queue.MessagePublisherImpl;
import net.irext.decoder.queue.MessageSubscriber;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
/**
* Filename: RedisConfig.java
* Revised: Date: 2018-12-29
* Revision: Revision: 1.0
* <p>
* Description: Redis config class
* <p>
* Revision log:
* 2018-12-29: created by strawmanbobi
*/
@Configuration
@ComponentScan("net.irext")
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(jedisConnectionFactory());
template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
return template;
}
@Bean
MessageListenerAdapter messageListener() {
return new MessageListenerAdapter(new MessageSubscriber());
}
@Bean
RedisMessageListenerContainer redisContainer() {
final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(jedisConnectionFactory());
container.addMessageListener(messageListener(), topic());
return container;
}
@Bean
MessagePublisher redisPublisher() {
return new MessagePublisherImpl(redisTemplate(), topic());
}
@Bean
ChannelTopic topic() {
return new ChannelTopic("pubsub:queue");
}
}

View File

@@ -0,0 +1,42 @@
package net.irext.decoder.model;
/**
* Filename: DecodeSession.java
* Revised: Date: 2018-12-29
* Revision: Revision: 1.0
* <p>
* Description: Decode session for decoding within connection
* <p>
* Revision log:
* 2018-12-29: created by strawmanbobi
*/
public class DecodeSession {
private Integer id;
private String name;
public DecodeSession(Integer id, String name) {
this.id = id;
this.name = name;
}
public DecodeSession() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@@ -0,0 +1,17 @@
package net.irext.decoder.queue;
/**
* Filename: MessagePublisher.java
* Revised: Date: 2018-12-29
* Revision: Revision: 1.0
* <p>
* Description: Message publisher for redis connection
* <p>
* Revision log:
* 2018-12-29: created by strawmanbobi
*/
public interface MessagePublisher {
void publish(final String message);
}

View File

@@ -0,0 +1,38 @@
package net.irext.decoder.queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.stereotype.Service;
/**
* Filename: MessagePublisherImpl.java
* Revised: Date: 2018-12-29
* Revision: Revision: 1.0
* <p>
* Description: Message publisher for redis connection
* <p>
* Revision log:
* 2018-12-29: created by strawmanbobi
*/
@Service
public class MessagePublisherImpl implements MessagePublisher {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private ChannelTopic topic;
public MessagePublisherImpl() {
}
public MessagePublisherImpl(final RedisTemplate<String, Object> redisTemplate, final ChannelTopic topic) {
this.redisTemplate = redisTemplate;
this.topic = topic;
}
public void publish(final String message) {
redisTemplate.convertAndSend(topic.getTopic(), message);
}
}

View File

@@ -0,0 +1,30 @@
package net.irext.decoder.queue;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Service;
/**
* Filename: MessageSubscriber.java
* Revised: Date: 2018-12-29
* Revision: Revision: 1.0
* <p>
* Description: Message subscriber for redis connection
* <p>
* Revision log:
* 2018-12-29: created by strawmanbobi
*/
@Service
public class MessageSubscriber implements MessageListener {
public static List<String> messageList = new ArrayList<String>();
public void onMessage(final Message message, final byte[] pattern) {
messageList.add(message.toString());
System.out.println("Message received: " + new String(message.getBody()));
}
}

View File

@@ -0,0 +1,27 @@
package net.irext.decoder.redisrepo;
import net.irext.decoder.model.DecodeSession;
import java.util.Map;
/**
* Filename: RedisRepository.java
* Revised: Date: 2018-12-29
* Revision: Revision: 1.0
* <p>
* Description: Redis cache class
* <p>
* Revision log:
* 2018-12-29: created by strawmanbobi
*/
public interface RedisRepository {
Map<Object, Object> findAllDecodeSessions();
void add(DecodeSession decodeSession);
void delete(Integer id);
DecodeSession find(Integer id);
}

View File

@@ -0,0 +1,56 @@
package net.irext.decoder.redisrepo;
import net.irext.decoder.model.DecodeSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import java.util.Map;
/**
* Filename: RedisRepositoryImpl.java
* Revised: Date: 2018-12-29
* Revision: Revision: 1.0
* <p>
* Description: Redis cache class
* <p>
* Revision log:
* 2018-12-29: created by strawmanbobi
*/
@Repository
public class RedisRepositoryImpl implements RedisRepository {
private static final String KEY = "SESSION_KEY";
private RedisTemplate<String, Object> redisTemplate;
private HashOperations hashOperations;
@Autowired
public RedisRepositoryImpl(RedisTemplate<String, Object> redisTemplate){
this.redisTemplate = redisTemplate;
}
@PostConstruct
private void init(){
hashOperations = redisTemplate.opsForHash();
}
public void add(final DecodeSession decodeSession) {
hashOperations.put(KEY, decodeSession.getId(), decodeSession.getName());
}
public void delete(final Integer id) {
hashOperations.delete(KEY, id);
}
public DecodeSession find(final Integer id){
return (DecodeSession) hashOperations.get(KEY, id);
}
public Map<Object, Object> findAllDecodeSessions(){
return hashOperations.entries(KEY);
}
}

View File

@@ -1,5 +1,6 @@
server.port=8082
spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379