Skip to content
This repository was archived by the owner on Oct 26, 2023. It is now read-only.

Commit 14865d0

Browse files
committed
* 'master' of https://github.com/SpringSource/spring-data-book: More example code for Redis chapter covering PubSub and Caching.
2 parents 9ceebc6 + b68b25b commit 14865d0

File tree

11 files changed

+409
-42
lines changed

11 files changed

+409
-42
lines changed

jpa/src/main/java/com/oreilly/springdata/jpa/ApplicationConfig.java

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
/**
3636
* Spring JavaConfig configuration class to setup a Spring container and infrastructure components like a
3737
* {@link DataSource}, a {@link EntityManagerFactory} and a {@link PlatformTransactionManager}.
38-
*
38+
*
3939
* @author Oliver Gierke
4040
*/
4141
@Configuration
@@ -44,52 +44,50 @@
4444
@EnableTransactionManagement
4545
class ApplicationConfig {
4646

47-
/**
48-
* Bootstraps an in-memory HSQL database.
49-
*
50-
* @see http
51-
* ://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/jdbc.html#jdbc-embedded-database
52-
* -support
53-
* @return
54-
*/
55-
@Bean
56-
public DataSource dataSource() {
57-
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
58-
return builder.setType(EmbeddedDatabaseType.HSQL).build();
59-
}
47+
/**
48+
* Bootstraps an in-memory HSQL database.
49+
*
50+
* @return
51+
* @see http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/jdbc.html#jdbc-embedded-database-support
52+
*/
53+
@Bean
54+
public DataSource dataSource() {
55+
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
56+
return builder.setType( EmbeddedDatabaseType.HSQL ).build();
57+
}
6058

61-
/**
62-
* Sets up a {@link LocalContainerEntityManagerFactoryBean} to use Hibernate. Activates picking up entities from the
63-
* project's base package.
64-
*
65-
* @return
66-
*/
67-
@Bean
68-
public EntityManagerFactory entityManagerFactory() {
59+
/**
60+
* Sets up a {@link LocalContainerEntityManagerFactoryBean} to use Hibernate. Activates picking up entities from the
61+
* project's base package.
62+
*
63+
* @return
64+
*/
65+
@Bean
66+
public EntityManagerFactory entityManagerFactory() {
6967

70-
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
71-
vendorAdapter.setGenerateDdl(true);
68+
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
69+
vendorAdapter.setGenerateDdl( true );
7270

73-
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
74-
factory.setJpaVendorAdapter(vendorAdapter);
75-
factory.setPackagesToScan(getClass().getPackage().getName());
76-
factory.setDataSource(dataSource());
71+
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
72+
factory.setJpaVendorAdapter( vendorAdapter );
73+
factory.setPackagesToScan( getClass().getPackage().getName() );
74+
factory.setDataSource( dataSource() );
7775

78-
factory.afterPropertiesSet();
76+
factory.afterPropertiesSet();
7977

80-
return factory.getObject();
81-
}
78+
return factory.getObject();
79+
}
8280

83-
@Bean
84-
public JpaDialect jpaDialect() {
85-
return new HibernateJpaDialect();
86-
}
81+
@Bean
82+
public JpaDialect jpaDialect() {
83+
return new HibernateJpaDialect();
84+
}
8785

88-
@Bean
89-
public PlatformTransactionManager transactionManager() {
86+
@Bean
87+
public PlatformTransactionManager transactionManager() {
9088

91-
JpaTransactionManager txManager = new JpaTransactionManager();
92-
txManager.setEntityManagerFactory(entityManagerFactory());
93-
return txManager;
94-
}
89+
JpaTransactionManager txManager = new JpaTransactionManager();
90+
txManager.setEntityManagerFactory( entityManagerFactory() );
91+
return txManager;
92+
}
9593
}

redis/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
dependencies {
22

33
runtime "ch.qos.logback:logback-classic:$logbackVersion"
4+
runtime "cglib:cglib:2.2.2"
45

56
// Spring
67
compile "org.springframework:spring-tx:$springVersion"
78
compile "org.springframework:spring-web:$springVersion"
89

910
// Spring Data Redis
10-
compile "org.springframework.data:spring-data-redis:1.0.0.RELEASE"
11+
compile "org.springframework.data:spring-data-redis:1.0.1.RELEASE"
12+
// compile("org.springframework.data:spring-data-redis:1.0.1.RELEASE") {
13+
// exclude module: "slf4j-log4j12"
14+
// exclude module: "log4j"
15+
// }
1116

1217
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.oreilly.springdata.redis;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.data.redis.connection.RedisConnectionFactory;
5+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
6+
import org.springframework.data.redis.core.RedisTemplate;
7+
import org.springframework.data.redis.serializer.RedisSerializer;
8+
import org.springframework.data.redis.serializer.SerializationException;
9+
10+
/**
11+
* @author Jon Brisbin
12+
*/
13+
public abstract class ApplicationConfig {
14+
15+
@Bean public RedisConnectionFactory redisConnectionFactory() {
16+
JedisConnectionFactory cf = new JedisConnectionFactory();
17+
cf.setHostName( "localhost" );
18+
cf.setPort( 6379 );
19+
cf.afterPropertiesSet();
20+
return cf;
21+
}
22+
23+
@Bean public RedisTemplate redisTemplate() {
24+
RedisTemplate rt = new RedisTemplate();
25+
rt.setConnectionFactory( redisConnectionFactory() );
26+
return rt;
27+
}
28+
29+
public static enum StringSerializer implements RedisSerializer<String> {
30+
INSTANCE;
31+
32+
@Override public byte[] serialize( String s ) throws SerializationException {
33+
return (null != s ? s.getBytes() : new byte[0]);
34+
}
35+
36+
@Override public String deserialize( byte[] bytes ) throws SerializationException {
37+
if ( bytes.length > 0 ) {
38+
return new String( bytes );
39+
} else {
40+
return null;
41+
}
42+
}
43+
}
44+
45+
public static enum LongSerializer implements RedisSerializer<Long> {
46+
INSTANCE;
47+
48+
@Override public byte[] serialize( Long aLong ) throws SerializationException {
49+
if ( null != aLong ) {
50+
return aLong.toString().getBytes();
51+
} else {
52+
return new byte[0];
53+
}
54+
}
55+
56+
@Override public Long deserialize( byte[] bytes ) throws SerializationException {
57+
if ( bytes.length > 0 ) {
58+
return Long.parseLong( new String( bytes ) );
59+
} else {
60+
return null;
61+
}
62+
}
63+
}
64+
65+
public static enum IntSerializer implements RedisSerializer<Integer> {
66+
INSTANCE;
67+
68+
@Override public byte[] serialize( Integer i ) throws SerializationException {
69+
if ( null != i ) {
70+
return i.toString().getBytes();
71+
} else {
72+
return new byte[0];
73+
}
74+
}
75+
76+
@Override public Integer deserialize( byte[] bytes ) throws SerializationException {
77+
if ( bytes.length > 0 ) {
78+
return Integer.parseInt( new String( bytes ) );
79+
} else {
80+
return null;
81+
}
82+
}
83+
}
84+
85+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.oreilly.springdata.redis;
2+
3+
import static org.hamcrest.MatcherAssert.*;
4+
import static org.hamcrest.Matchers.*;
5+
6+
import org.junit.Test;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9+
10+
/**
11+
* @author Jon Brisbin
12+
*/
13+
public class ApplicationConfigTest {
14+
15+
@Test public void boostrapFromJavaConfig() {
16+
ApplicationContext appCtx = new AnnotationConfigApplicationContext(ApplicationConfig.class);
17+
18+
assertThat(appCtx, is(notNullValue()));
19+
}
20+
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.oreilly.springdata.redis;
2+
3+
import static org.hamcrest.MatcherAssert.*;
4+
import static org.hamcrest.Matchers.*;
5+
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.data.redis.connection.RedisConnectionFactory;
10+
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
11+
import org.springframework.test.context.ContextConfiguration;
12+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13+
14+
/**
15+
* @author Jon Brisbin
16+
*/
17+
@RunWith(SpringJUnit4ClassRunner.class)
18+
@ContextConfiguration(classes = {ApplicationConfig.class})
19+
public class AtomicCountersTest {
20+
21+
@Autowired RedisConnectionFactory connectionFactory;
22+
23+
@Test public void testAtomicCounters() {
24+
RedisAtomicLong counter = new RedisAtomicLong("spring-data-book:counter-test:hits", connectionFactory, 0);
25+
Long l = counter.incrementAndGet();
26+
27+
assertThat(l, is(greaterThan(0L)));
28+
}
29+
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.oreilly.springdata.redis;
2+
3+
import static org.hamcrest.MatcherAssert.*;
4+
import static org.hamcrest.Matchers.*;
5+
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.data.redis.connection.RedisConnectionFactory;
10+
import org.springframework.data.redis.core.RedisTemplate;
11+
import org.springframework.data.redis.core.ValueOperations;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
15+
/**
16+
* @author Jon Brisbin
17+
*/
18+
@RunWith(SpringJUnit4ClassRunner.class)
19+
@ContextConfiguration(classes = {ApplicationConfig.class})
20+
public class KeyValueSerializersTest {
21+
22+
@Autowired RedisConnectionFactory connectionFactory;
23+
24+
@Test public void testStringLongSerializers() {
25+
RedisTemplate<String, Long> redis = new RedisTemplate<String, Long>();
26+
redis.setConnectionFactory( connectionFactory );
27+
redis.setKeySerializer( ApplicationConfig.StringSerializer.INSTANCE );
28+
redis.setValueSerializer( ApplicationConfig.LongSerializer.INSTANCE );
29+
30+
ValueOperations<String, Long> ops = redis.opsForValue();
31+
32+
String key = "spring-data-book:counter-test:hits";
33+
34+
ops.setIfAbsent( key, 1L );
35+
Long l = ops.increment( key, 1 );
36+
37+
assertThat( l, is( greaterThan( 0L ) ) );
38+
}
39+
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.oreilly.springdata.redis;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.redis.connection.Message;
6+
import org.springframework.data.redis.connection.MessageListener;
7+
import org.springframework.data.redis.listener.ChannelTopic;
8+
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
9+
10+
/**
11+
* @author Jon Brisbin
12+
*/
13+
@Configuration
14+
public class PubSubConfig extends ApplicationConfig {
15+
16+
public static final String DUMP_CHANNEL = "spring-data-book:pubsub-test:dump";
17+
18+
@Bean RedisMessageListenerContainer container() {
19+
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
20+
container.setConnectionFactory(redisConnectionFactory());
21+
container.addMessageListener(dumpToConsoleListener(), new ChannelTopic(DUMP_CHANNEL));
22+
return container;
23+
}
24+
25+
@Bean MessageListener dumpToConsoleListener() {
26+
return new MessageListener() {
27+
@Override public void onMessage(Message message, byte[] pattern) {
28+
System.out.println("FROM MESSAGE: " + new String(message.getBody()));
29+
}
30+
};
31+
}
32+
33+
}

0 commit comments

Comments
 (0)