redis配置
在application.properties(总的配置文件)
相关依赖:rabbitMQ是spring底层提供了的;
redis.clients
jedis
2.9.0
com.alibaba
fastjson
1.2.54
#redis
redis.host=127.0.0.1
redis.port=6379
redis.timeout=3
#redis.password=123456
redis.poolMaxTotal=10
redis.poolMaxIdle=10
redis.poolMaxWait=3
然后下个redis的配置类,用来读取配置文件的数据;
@Component
@ConfigurationProperties(prefix="redis")
public class RedisConfig {
private String host;
private int port;
private int timeout;
private int poolMaxTotal;
private int poolMaxIdle;
private int poolMaxWait;
private String password;省略了set和get方法
下一步是用到连接Java和redis的连接jedis;
先定义一个类,生成一个jedis连接池,加入到spring容器;
@Autowired
RedisConfig redisConfig;
@Bean
public JedisPool jedisPoolFactory(){
JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
jedisPoolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
jedisPoolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait()*1000);
JedisPool jedisPool=new JedisPool(jedisPoolConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getTimeout()*1000, redisConfig.getPassword(), 0);
return jedisPool;
然后是写一个redis的service类;用来存值和取值;
因为jedis的键和值都是string类型,所以要用到类型转换的根据;
其中用到了fastson;根据一个类的反射来得到该类的实例对象;JSON.toJavaObject(JSON.parseObject(str), c);
还有类转String的
JSON.toJSONString(value)
@Autowired
JedisPool jedisPool;
public boolean set(String key,T value){
Jedis jedis=null;
try{
jedis=jedisPool.getResource();
String str=beanToString(value);
if(str==null){
return false;
}
jedis.set(key,str);
return true;
}finally {
returnToPool(jedis);
}
}
public T get(String key,Class c){
Jedis jedis=null;
try{
jedis=jedisPool.getResource();
String str=jedis.get(key);
T t=stringToBean(str,c);
if(str==null){
return null;
}
return t;
}finally {
returnToPool(jedis);
}
}
@SuppressWarnings("unchecked")
public static T stringToBean(String str, Class c) {
if(str==null||str.length()<=0||c==null){
return null;
}
if(int.class==c||Integer.class==c){
return (T) Integer.valueOf(str);
}
else if(String.class==c){
return (T) str;
}
else if(long.class==c||Long.class==c){
return (T) Long.valueOf(str);
}
else{
return JSON.toJavaObject(JSON.parseObject(str), c);
}
}
public static String beanToString(T value) {
if(value==null){
return null;
}
Class >c=value.getClass();
if(c==int.class||c==Integer.class){
return ""+value;
}
if(c==String.class){
return (String) value;
}
if(c==long.class||c==Long.class){
return ""+value;
}
if(c==double.class||c==Double.class){
return ""+value;
}
else{
return JSON.toJSONString(value);
}
}
private static void returnToPool(Jedis jedis) {
if(jedis!=null){
jedis.close();
}
}
config类用来注册队列,产生一个队列
@Configuration
public class MQConfig {
public final static String QUEU_ENAME="queuesss";
@Bean
public Queue queue(){
return new Queue(QUEU_ENAME,false);
}
}
Sender类用来将消息加入队列;
@Service
public class MQSender {
@Autowired
AmqpTemplate template;
public void send(Object message){
String msg=RedisService.beanToString(message);
template.convertAndSend(MQConfig.QUEU_ENAME, msg);
}
接收消息的类方法不能有返回值
@Service
public class MQReceiver {
@Autowired
GoodService goodService;
@Autowired
RedisService redis;
@RabbitListener(queues=MQConfig.QUEU_ENAME)
//方法不能有返回值
public void receive(String message){
}
rabbitMq可以通过浏览器访问本地客户端http://localhost:15672/#/queues