Skip to content

Commit 5c85ca3

Browse files
author
quding
committed
add redisDemo
1 parent 8512761 commit 5c85ca3

File tree

6 files changed

+241
-0
lines changed

6 files changed

+241
-0
lines changed

Spring-Data-Redis/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>cn.mrdear</groupId>
8+
<artifactId>Spring-Data-Redis</artifactId>
9+
<version>1.0.0</version>
10+
<name>Spring-Data-Redis</name>
11+
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>1.5.2.RELEASE</version>
16+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<java.version>1.8</java.version>
21+
</properties>
22+
23+
<dependencies>
24+
<!--spring boot start-->
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-redis</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.mrdear;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author Niu Li
8+
* @since 2017/3/28
9+
*/
10+
@SpringBootApplication
11+
public class Application {
12+
public static void main( String[] args )
13+
{
14+
SpringApplication.run(Application.class, args);
15+
}
16+
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.mrdear.publish;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.util.concurrent.CountDownLatch;
8+
9+
/**
10+
* 消息订阅者
11+
* @author Niu Li
12+
* @since 2017/3/29
13+
*/
14+
@Component
15+
public class Listen {
16+
17+
private static Logger logger = LoggerFactory.getLogger(Listen.class);
18+
19+
private CountDownLatch latch = new CountDownLatch(1);
20+
21+
public void handleMsg(String message) {
22+
logger.info("reciver msg :" + message);
23+
latch.countDown();
24+
}
25+
26+
public CountDownLatch getLatch() {
27+
return latch;
28+
}
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cn.mrdear.publish;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.redis.connection.RedisConnectionFactory;
6+
import org.springframework.data.redis.listener.PatternTopic;
7+
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
8+
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
9+
10+
/**
11+
* @author Niu Li
12+
* @since 2017/3/29
13+
*/
14+
@Configuration
15+
public class PublishConfig {
16+
/**
17+
* 注入消息容器
18+
* @param jedisConnectionFactory jedis连接池
19+
* @param listenerAdapter 监听适配器
20+
* @return bean
21+
*/
22+
@Bean
23+
public RedisMessageListenerContainer container(RedisConnectionFactory jedisConnectionFactory,
24+
MessageListenerAdapter listenerAdapter){
25+
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
26+
container.setConnectionFactory(jedisConnectionFactory);
27+
//绑定监听者与信道的管理
28+
container.addMessageListener(listenerAdapter,new PatternTopic("java"));
29+
return container;
30+
}
31+
32+
@Bean
33+
public MessageListenerAdapter adapter(Listen listen){
34+
//指定监听者和监听方法
35+
return new MessageListenerAdapter(listen,"handleMsg");
36+
}
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#logger start
2+
logging.path = logs/loging.log
3+
logging.level.cn.mrdear = debug
4+
#logger end
5+
6+
# spring date redis start
7+
# Redis数据库索引(默认为0)
8+
spring.redis.database=0
9+
# Redis服务器地址
10+
spring.redis.host=115.159.185.14
11+
# Redis服务器连接端口
12+
spring.redis.port=6379
13+
# Redis服务器连接密码(默认为空)
14+
spring.redis.password=
15+
# 连接池最大连接数(使用负值表示没有限制)
16+
spring.redis.pool.max-active=8
17+
# 连接池最大阻塞等待时间(使用负值表示没有限制)
18+
spring.redis.pool.max-wait=-1
19+
# 连接池中的最大空闲连接
20+
spring.redis.pool.max-idle=8
21+
# 连接池中的最小空闲连接
22+
spring.redis.pool.min-idle=0
23+
# 连接超时时间(毫秒)
24+
spring.redis.timeout=2000
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cn.mrdear.test;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.dao.DataAccessException;
7+
import org.springframework.data.redis.connection.RedisConnection;
8+
import org.springframework.data.redis.connection.StringRedisConnection;
9+
import org.springframework.data.redis.core.RedisCallback;
10+
import org.springframework.data.redis.core.RedisOperations;
11+
import org.springframework.data.redis.core.SessionCallback;
12+
import org.springframework.data.redis.core.StringRedisTemplate;
13+
import org.springframework.data.redis.core.ValueOperations;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
16+
import java.util.List;
17+
18+
import javax.annotation.Resource;
19+
20+
import cn.mrdear.Application;
21+
import cn.mrdear.publish.Listen;
22+
23+
/**
24+
* @author Niu Li
25+
* @since 2017/3/28
26+
*/
27+
@RunWith(SpringJUnit4ClassRunner.class)
28+
@SpringBootTest(classes = Application.class)
29+
public class RedisConnectTest {
30+
@Resource
31+
private StringRedisTemplate stringRedisTemplate;
32+
@Resource
33+
private Listen listen;
34+
@Test
35+
public void testSetAndGet() {
36+
stringRedisTemplate.opsForValue().set("ping","pong");
37+
System.out.println(stringRedisTemplate.opsForValue().get("ping"));
38+
}
39+
40+
@Test
41+
public void testPublish() throws InterruptedException {
42+
stringRedisTemplate.convertAndSend("java","hello world");
43+
listen.getLatch().await();
44+
}
45+
46+
@Test
47+
public void testMulti() {
48+
boolean isThrow = false;
49+
List<Object> result = null;
50+
try {
51+
result = stringRedisTemplate.execute(new SessionCallback<List<Object>>() {
52+
@Override
53+
public List<Object> execute(RedisOperations operations) throws
54+
DataAccessException {
55+
operations.multi();
56+
ValueOperations<String,String> ops = operations.opsForValue();
57+
ops.set("ping1","pong1");
58+
ops.set("ping2","pong2");
59+
if (isThrow){
60+
throw new IllegalArgumentException("测试异常");
61+
}
62+
return operations.exec();
63+
}
64+
});
65+
} catch (Exception e) {
66+
e.printStackTrace();
67+
}
68+
69+
System.out.println(result);
70+
}
71+
72+
@Test
73+
public void testPipe() {
74+
//pop a specified number of items from a queue
75+
List<Object> results = stringRedisTemplate.executePipelined(
76+
new RedisCallback<Object>() {
77+
public Object doInRedis(RedisConnection connection) throws DataAccessException {
78+
StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
79+
for(int i=0; i< 10; i++) {
80+
stringRedisConn.rPush("myqueue",i+"");
81+
}
82+
return null;
83+
}
84+
});
85+
}
86+
}

0 commit comments

Comments
 (0)