forked from mrdear/JavaWEB
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
quding
committed
Mar 30, 2017
1 parent
8512761
commit 5c85ca3
Showing
6 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>cn.mrdear</groupId> | ||
<artifactId>Spring-Data-Redis</artifactId> | ||
<version>1.0.0</version> | ||
<name>Spring-Data-Redis</name> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.2.RELEASE</version> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<!--spring boot start--> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-redis</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
17 changes: 17 additions & 0 deletions
17
Spring-Data-Redis/src/main/java/cn/mrdear/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.mrdear; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* @author Niu Li | ||
* @since 2017/3/28 | ||
*/ | ||
@SpringBootApplication | ||
public class Application { | ||
public static void main( String[] args ) | ||
{ | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
Spring-Data-Redis/src/main/java/cn/mrdear/publish/Listen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cn.mrdear.publish; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
/** | ||
* 消息订阅者 | ||
* @author Niu Li | ||
* @since 2017/3/29 | ||
*/ | ||
@Component | ||
public class Listen { | ||
|
||
private static Logger logger = LoggerFactory.getLogger(Listen.class); | ||
|
||
private CountDownLatch latch = new CountDownLatch(1); | ||
|
||
public void handleMsg(String message) { | ||
logger.info("reciver msg :" + message); | ||
latch.countDown(); | ||
} | ||
|
||
public CountDownLatch getLatch() { | ||
return latch; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
Spring-Data-Redis/src/main/java/cn/mrdear/publish/PublishConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cn.mrdear.publish; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.listener.PatternTopic; | ||
import org.springframework.data.redis.listener.RedisMessageListenerContainer; | ||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; | ||
|
||
/** | ||
* @author Niu Li | ||
* @since 2017/3/29 | ||
*/ | ||
@Configuration | ||
public class PublishConfig { | ||
/** | ||
* 注入消息容器 | ||
* @param jedisConnectionFactory jedis连接池 | ||
* @param listenerAdapter 监听适配器 | ||
* @return bean | ||
*/ | ||
@Bean | ||
public RedisMessageListenerContainer container(RedisConnectionFactory jedisConnectionFactory, | ||
MessageListenerAdapter listenerAdapter){ | ||
RedisMessageListenerContainer container = new RedisMessageListenerContainer(); | ||
container.setConnectionFactory(jedisConnectionFactory); | ||
//绑定监听者与信道的管理 | ||
container.addMessageListener(listenerAdapter,new PatternTopic("java")); | ||
return container; | ||
} | ||
|
||
@Bean | ||
public MessageListenerAdapter adapter(Listen listen){ | ||
//指定监听者和监听方法 | ||
return new MessageListenerAdapter(listen,"handleMsg"); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Spring-Data-Redis/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#logger start | ||
logging.path = logs/loging.log | ||
logging.level.cn.mrdear = debug | ||
#logger end | ||
|
||
# spring date redis start | ||
# Redis数据库索引(默认为0) | ||
spring.redis.database=0 | ||
# Redis服务器地址 | ||
spring.redis.host=115.159.185.14 | ||
# Redis服务器连接端口 | ||
spring.redis.port=6379 | ||
# Redis服务器连接密码(默认为空) | ||
spring.redis.password= | ||
# 连接池最大连接数(使用负值表示没有限制) | ||
spring.redis.pool.max-active=8 | ||
# 连接池最大阻塞等待时间(使用负值表示没有限制) | ||
spring.redis.pool.max-wait=-1 | ||
# 连接池中的最大空闲连接 | ||
spring.redis.pool.max-idle=8 | ||
# 连接池中的最小空闲连接 | ||
spring.redis.pool.min-idle=0 | ||
# 连接超时时间(毫秒) | ||
spring.redis.timeout=2000 |
86 changes: 86 additions & 0 deletions
86
Spring-Data-Redis/src/test/java/cn/mrdear/test/RedisConnectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cn.mrdear.test; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.data.redis.connection.RedisConnection; | ||
import org.springframework.data.redis.connection.StringRedisConnection; | ||
import org.springframework.data.redis.core.RedisCallback; | ||
import org.springframework.data.redis.core.RedisOperations; | ||
import org.springframework.data.redis.core.SessionCallback; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import java.util.List; | ||
|
||
import javax.annotation.Resource; | ||
|
||
import cn.mrdear.Application; | ||
import cn.mrdear.publish.Listen; | ||
|
||
/** | ||
* @author Niu Li | ||
* @since 2017/3/28 | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@SpringBootTest(classes = Application.class) | ||
public class RedisConnectTest { | ||
@Resource | ||
private StringRedisTemplate stringRedisTemplate; | ||
@Resource | ||
private Listen listen; | ||
@Test | ||
public void testSetAndGet() { | ||
stringRedisTemplate.opsForValue().set("ping","pong"); | ||
System.out.println(stringRedisTemplate.opsForValue().get("ping")); | ||
} | ||
|
||
@Test | ||
public void testPublish() throws InterruptedException { | ||
stringRedisTemplate.convertAndSend("java","hello world"); | ||
listen.getLatch().await(); | ||
} | ||
|
||
@Test | ||
public void testMulti() { | ||
boolean isThrow = false; | ||
List<Object> result = null; | ||
try { | ||
result = stringRedisTemplate.execute(new SessionCallback<List<Object>>() { | ||
@Override | ||
public List<Object> execute(RedisOperations operations) throws | ||
DataAccessException { | ||
operations.multi(); | ||
ValueOperations<String,String> ops = operations.opsForValue(); | ||
ops.set("ping1","pong1"); | ||
ops.set("ping2","pong2"); | ||
if (isThrow){ | ||
throw new IllegalArgumentException("测试异常"); | ||
} | ||
return operations.exec(); | ||
} | ||
}); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
System.out.println(result); | ||
} | ||
|
||
@Test | ||
public void testPipe() { | ||
//pop a specified number of items from a queue | ||
List<Object> results = stringRedisTemplate.executePipelined( | ||
new RedisCallback<Object>() { | ||
public Object doInRedis(RedisConnection connection) throws DataAccessException { | ||
StringRedisConnection stringRedisConn = (StringRedisConnection)connection; | ||
for(int i=0; i< 10; i++) { | ||
stringRedisConn.rPush("myqueue",i+""); | ||
} | ||
return null; | ||
} | ||
}); | ||
} | ||
} |