본문 바로가기
Spring

Spring 4.3 Redis Sentinel Pubsub 설정

by 수앙 2018. 6. 16.

1. Spring redis 연동 설정은 아래 참고

http://sooin01.tistory.com/entry/Spring-4-Redis-Sentinel-%EC%97%B0%EB%8F%99


2. Spring redis pubsub xml 설정

<bean id="messageListenerAdapter" class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">

<constructor-arg>

<!-- 사용자 정의 클래스(메시지 리스너) -->

<bean class="aaa.bbb.ccc.RedisMessageListener" />

</constructor-arg>

</bean>


<bean id="channelTopic" class="org.springframework.data.redis.listener.ChannelTopic">

<!-- 사용자가 원하는 value 값으로 설정 -->

<constructor-arg value="pubsub:message" />

</bean>


<bean id="redisMessageListenerContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer">

<property name="connectionFactory" ref="jedisConnectionFactory" />

<property name="messageListeners">

<map>

<entry key-ref="messageListenerAdapter" value-ref="channelTopic" />

</map>

</property>

</bean>


3. 사용자 정의 클래스(메시지 리스너)

package aaa.bbb.cccr;


import java.io.UnsupportedEncodingException;


import org.springframework.data.redis.connection.Message;

import org.springframework.data.redis.connection.MessageListener;


public class RedisMessageListener implements MessageListener {


@Override

public void onMessage(Message message, byte[] pattern) {

try {

System.out.println("Message received: " + message + ", " + new String(pattern, "UTF-8"));

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}


}



4. Spring test 코드

import javax.annotation.Resource;


import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.ListOperations;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ValueOperations;

import org.springframework.data.redis.listener.ChannelTopic;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:spring/*.xml" })

public class RedisConnectionTest {


@Autowired

private RedisTemplate<String, Object> redisTemplate;


@Autowired

private ChannelTopic channelTopic;


@Test

public void testPublish() throws Exception {

for (int i = 0; i < 10; i++) {

redisTemplate.convertAndSend(channelTopic.getTopic(), "message " + i);

Thread.sleep(1000);

}


Thread.sleep(5000);

}


}



5. 결과


끝.

'Spring' 카테고리의 다른 글

Spring 4 interceptor custom annotation  (0) 2019.11.13
Spring 4 request logging  (0) 2019.11.07
Spring 4.3 Redis Sentinel 연동  (0) 2018.06.16
Spring 4 Atomikos 설정  (0) 2017.10.14
Spring 4 JSR-303 Validator  (0) 2017.06.12

댓글