spring---消息订阅发布之redis

  消息的传递在分布式开发中我们常常会遇到,接下来的三篇文章分别介绍spring与redis、JMS、activeMQ的结合使用。三种方式都使用订阅发布的模式。首先我们来看一下与redis的结合使用。

redis准备

首先,我们先下载和安装redis,并启动redis-server。
redis下载地址

tar zxvf redis-3.2.0.tar.gz
cd redis-3.2.0
make
cd src
./redis-server

编码

首先,我们在pom.xml中添加如下redis支持jar包:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.4.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.4.2</version>
</dependency>

然后定义接收信息的Receiver:

package redis;

import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Receiver {
    private static final Logger LOGGER =LoggerFactory.getLogger(Receiver.class);

    private CountDownLatch latch;

    public Receiver(CountDownLatch latch){
        this.latch = latch;
    }

    public void receiveMessage(String message){
        LOGGER.info("Received <"+ message + ">");
        latch.countDown();
    }
}

在这里,Receiver只是定义了接收消息的方法,并使用CountDownLatch来保证信息接收完成。接下来定义程序运行的主类Application:

package redis;

import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@SpringBootApplication
public class Application{
    private static final Logger  log = LoggerFactory.getLogger(Application.class);

    @Bean
    RedisMessageListenerContainer contain(RedisConnectionFactory factory, MessageListenerAdapter listenerContainer){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        container.addMessageListener(listenerContainer, new PatternTopic("chat"));

        return container;
    }

    @Bean
    Receiver receiver(CountDownLatch latch){
        return new Receiver(latch);
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver){
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    @Bean
    CountDownLatch latch(){
        return new CountDownLatch(1);
    }

    @Bean
    StringRedisTemplate template(RedisConnectionFactory connectionFactory){
        return new StringRedisTemplate(connectionFactory);
    }

    @Bean
    RedisConnectionFactory factory(){
        return new JedisConnectionFactory();
    }

    public static void main(String[] args) throws InterruptedException {
        ApplicationContext ctx =    SpringApplication.run(Application.class,args);
        StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
        CountDownLatch latch = ctx.getBean(CountDownLatch.class);

        log.info("sending message ...");

        template.convertAndSend("chat", "hello from redis!");
        latch.await();
        System.exit(0);
    }

}

在上面的代码中,我们可以注意到使用了一个@SpringBootApplication注释,这个注释包含了以下注释功能,也可以用以下注释替换掉@SpringBootApplication。

@Configuration 
@EnableAutoConfiguration
@ComponentScan
@EnableWebMvc

运行一下程序就可以看到,消息的收发了:

2016-06-02 21:53:38.375  INFO 12960 --- [           main] redis.Application                        : sending message ...
2016-06-02 21:53:38.384  INFO 12960 --- [      contain-2] redis.Receiver                           : Received <hello from redis!>

参考:https://spring.io/guides

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值