1. JDBC
配置:
<!-- 配置spring的JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
需要用到jdbc是注入:
@Autowired
private JdbcTemplate jdbcTemplate;
2. redis
配置:
<!-- redis -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.1.140" />
<constructor-arg name="port" value="6379" />
</bean>
<bean id="jedisClientPool" class="com.pro.huanbao.jedis.JedisClientPool" />
工具类:
接口:
package com.pro.huanbao.jedis;
import java.util.List;
import java.util.Set;
public interface JedisClient {
String set(String key, String value);
String get(String key);
Boolean exists(String key);
Long expire(String key, int seconds);
Long ttl(String key);
Long incr(String key);
Long hset(String key, String field, String value);
String hget(String key, String field);
Long hdel(String key, String... field);
/**
* list集合操作
*/
// 添加
Long lpush(String key, String value);
// 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
Long lrem(String key, long count, String field);
// 删除下标区间之外的元素 (0-3)
String ltrim(String key ,long start, long end);
// 列表元素出栈
String lpop(String key);
// 修改列表中指定下标的值
String lset(String key, long index, String field);
// 数组长度
Long llen(String key);
// 查询所有元素
List<String> lrange(String key);
// 查询指定区间元素
List<String> lrangeSE(String key, long start, long end);
/**
* set集合操作
*/
// 添加元素到set集合中
Long sadd(String key , String... field);
// 查询set集合所有元素
Set<String> smembers(String key);
// 删除元素
Long srem(String key, String field);
// 判断元素是否存在集合中
Boolean sismember(String key, String field);
// 查看两个集合的交集
Set<String> sinter(String key, String key2);
// 查看两个集合的并集
Set<String> sunion(String key, String key2);
// 查看两个集合的差集
Set<String> sdiff(String key, String key2);
}
实现类:
package com.pro.huanbao.jedis;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class JedisClientPool implements JedisClient {
@Autowired
private JedisPool jedisPool;
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String result = jedis.set(key, value);
jedis.close();
return result;
}
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String result = jedis.get(key);
jedis.close();
return result;
}
@Override
public Boolean exists(String key) {
Jedis jedis = jedisPool.getResource();
Boolean result = jedis.exists(key);
jedis.close();
return result;
}
@Override
public Long expire(String key, int seconds) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, seconds);
jedis.close();
return result;
}
@Override
public Long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return result;
}
@Override
public Long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
@Override
public Long hset(String key, String field, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(key, field, value);
jedis.close();
return result;
}
@Override
public String hget(String key, String field) {
Jedis jedis = jedisPool.getResource();
String result = jedis.hget(key, field);
jedis.close();
return result;
}
@Override
public Long hdel(String key, String... field) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(key, field);
jedis.close();
return result;
}
@Override
public Long sadd(String key, String... field) {
Jedis jedis = jedisPool.getResource();
Long sadd = jedis.sadd(key, field);
jedis.close();
return sadd;
}
@Override
public Set<String> smembers(String key) {
Jedis jedis = jedisPool.getResource();
Set<String> smembers = jedis.smembers(key);
jedis.close();
return smembers;
}
@Override
public Long srem(String key, String field) {
Jedis jedis = jedisPool.getResource();
Long srem = jedis.srem(key, field);
jedis.close();
return srem;
}
@Override
public Boolean sismember(String key ,String field) {
Jedis jedis = jedisPool.getResource();
Boolean sismember = jedis.sismember(key, field);
jedis.close();
return sismember;
}
@Override
public Set<String> sinter(String key, String key2) {
Jedis jedis = jedisPool.getResource();
Set<String> sinter = jedis.sinter(key, key2);
jedis.close();
return sinter;
}
@Override
public Set<String> sunion(String key, String key2) {
Jedis jedis = jedisPool.getResource();
Set<String> sunion = jedis.sunion(key, key2);
jedis.close();
return sunion;
}
@Override
public Set<String> sdiff(String key, String key2) {
Jedis jedis = jedisPool.getResource();
Set<String> sdiff = jedis.sdiff(key, key2);
jedis.close();
return sdiff;
}
@Override
public Long lpush(String key, String value) {
Jedis jedis = jedisPool.getResource();
Long lpush = jedis.lpush(key, value);
jedis.close();
return lpush;
}
@Override
public Long lrem(String key, long count, String field) {
Jedis jedis = jedisPool.getResource();
Long lrem = jedis.lrem(key, count, field);
jedis.close();
return lrem;
}
@Override
public String ltrim(String key, long start, long end) {
Jedis jedis = jedisPool.getResource();
String ltrim = jedis.ltrim(key, start, end);
jedis.close();
return ltrim;
}
@Override
public String lpop(String key) {
Jedis jedis = jedisPool.getResource();
String lpop = jedis.lpop(key);
jedis.close();
return lpop;
}
@Override
public String lset(String key, long index, String field) {
Jedis jedis = jedisPool.getResource();
String lset = jedis.lset(key, index, field);
jedis.close();
return lset;
}
@Override
public Long llen(String key) {
Jedis jedis = jedisPool.getResource();
Long llen = jedis.llen(key);
jedis.close();
return llen;
}
@Override
public List<String> lrange(String key) {
Jedis jedis = jedisPool.getResource();
List<String> lrange = jedis.lrange(key, 0, -1);
jedis.close();
return lrange;
}
@Override
public List<String> lrangeSE(String key, long start, long end) {
Jedis jedis = jedisPool.getResource();
List<String> lrange = jedis.lrange(key, start, end);
jedis.close();
return null;
}
}
3. ActiveMQ
配置:
生产者:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.1.140:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
<!-- 配置生产者 -->
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!--这个是队列目的地,点对点的 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue-message</value>
</constructor-arg>
</bean>
<!--这个是主题目的地,一对多的 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic" />
</bean>
</beans>
消费者:
<!--这个是队列目的地,点对点的 -->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue-message</value>
</constructor-arg>
</bean>
<bean id="queueDestination2" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue-path</value>
</constructor-arg>
</bean>
<bean id="queueDestination3" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>queue-recommend</value>
</constructor-arg>
</bean>
<!--这个是主题目的地,一对多的 -->
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic" />
</bean>
<!-- 配置消息监听器 -->
<bean id="SendMessageListener" class="com.pro.huanbao.listener.SendMessageListener" />
<bean id="RecommendSystemListener" class="com.pro.huanbao.listener.RecommendSystemListener" />
<!-- 配置监听容器 -->
<!-- 消息监听容器 -->
<bean
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination" />
<property name="messageListener" ref="SendMessageListener" />
</bean>
<!-- 消息监听容器 -->
<bean
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="queueDestination3" />
<property name="messageListener" ref="RecommendSystemListener" />
</bean>
4. dubbo
暴露接口:
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="huanbao-manager" />
<dubbo:registry protocol="zookeeper" address="192.168.1.140:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.pro.huanbao.service.CompanyService"
ref="companyServiceImpl" timeout="3000000" />
引用接口:
<!-- 引用dubbo服务 -->
<dubbo:application name="huanbao-manager-web" />
<dubbo:registry protocol="zookeeper" address="192.168.1.140:2181" />
<dubbo:reference interface="com.pro.huanbao.service.CompanyService"
id="companyService" />
5. quartz
配置:
<!-- 配置作业类 -->
<bean id="clearIndexCache"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<bean class="com.pro.huanbao.job.ClearIndexCache" />
</property>
<property name="targetMethod" value="clearIndexCache" />
<property name="concurrent" value="false" /><!-- 作业不并发调度 -->
</bean>
<!-- 配置作业调度的触发方式(触发器) -->
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="clearIndexCache" />
<property name="startDelay" value="20" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="1800000" /><!-- 每2秒调度一次 -->
</bean>
<!-- 配置调度工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
工作类:
package com.pro.huanbao.job;
import org.springframework.stereotype.Controller;
@Controller
public class ClearIndexCache {
/**
* 工作方法
* @Title clearIndexCache
* @param
* @return void
*/
public void clearIndexCache() {
// xxx
}
}