Authored by 胡古飞

添加通用过滤类

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
... ...
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yoho.dsf</groupId>
<artifactId>yoho-message-sender</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.yoho.dsf.yhmessagesender</groupId>
<artifactId>yoho-message-sender-common</artifactId>
<packaging>jar</packaging>
<name>yoho-message-sender-common</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.yoho.dsf.yhmessagesender</groupId>
<artifactId>yoho-message-sender-dal</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.service.model</groupId>
<artifactId>message-service-model</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-redis</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-message</artifactId>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.yoho.common</groupId>
<artifactId>error-code</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20150729</version>
</dependency>
<dependency>
<groupId>com.cloopen</groupId>
<artifactId>CCP_REST_SMS_SDK_JAVA</artifactId>
<version>v2.6.3r</version>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-cache-clean</artifactId>
</dependency>
<dependency>
<groupId>com.xuanwu</groupId>
<artifactId>postmsg-ump</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.0.8</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-sms</artifactId>
<version>3.0.0-rc1</version>
</dependency>
</dependencies>
<build>
<finalName>yoho-message-sender-sms</finalName>
</build>
</project>
... ...
package com.yoho.yhmessage.sms.filter;
package com.yoho.yhmessage.filter;
import java.util.List;
import java.util.concurrent.TimeUnit;
... ...
package com.yoho.yhmessage.sms.filter;
package com.yoho.yhmessage.filter;
public interface IObjectKeyTransfer<V> {
... ...
package com.yoho.yhmessage.sms.filter.impl;
package com.yoho.yhmessage.filter.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -17,10 +15,9 @@ import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import com.yoho.core.redis.YHRedisTemplate;
import com.yoho.yhmessage.sms.filter.IFilterByCountService;
import com.yoho.yhmessage.sms.filter.IObjectKeyTransfer;
import com.yoho.yhmessage.filter.IFilterByCountService;
import com.yoho.yhmessage.filter.IObjectKeyTransfer;
import com.yoho.yhmessage.redis.PushRedisService;
@Service
public class FilterByCountServiceImpl implements IFilterByCountService {
... ... @@ -28,8 +25,7 @@ public class FilterByCountServiceImpl implements IFilterByCountService {
private static final Logger logger = LoggerFactory.getLogger(FilterByCountServiceImpl.class);
@Autowired
@Resource(name = "yhRedisTemplate")
private YHRedisTemplate<String, String> yhRedisTemplate;
private PushRedisService pushRedisService;
@Override
public <V> List<V> batchFilter(String redisKey, List<V> objects, List<V> whiteObjects, IObjectKeyTransfer<V> objectKeyTransfer, int maxCount, long timeout, TimeUnit unit) {
... ... @@ -53,7 +49,7 @@ public class FilterByCountServiceImpl implements IFilterByCountService {
timeout = 1;
unit = TimeUnit.DAYS;
}
BoundHashOperations<String, String, String> operations = yhRedisTemplate.boundHashOps(redisKey);
BoundHashOperations<String, String, String> operations = pushRedisService.getPushRedisTemplate().boundHashOps(redisKey);
if (operations.getExpire() <= 0) {
operations.expire(timeout, unit);
}
... ... @@ -86,11 +82,11 @@ public class FilterByCountServiceImpl implements IFilterByCountService {
*/
private List<Integer> bacthIncrCount(String redisKey, List<String> objectKeys, int incCount) {
try {
List<Integer> increasedResults = yhRedisTemplate.execute(new RedisCallback<List<Integer>>() {
List<Integer> increasedResults = pushRedisService.getPushRedisTemplate().execute(new RedisCallback<List<Integer>>() {
@Override
public List<Integer> doInRedis(RedisConnection redisConnection) throws DataAccessException {
redisConnection.openPipeline();
RedisSerializer<String> stringSerializer = yhRedisTemplate.getStringSerializer();
RedisSerializer<String> stringSerializer = pushRedisService.getPushRedisTemplate().getStringSerializer();
byte[] key = null;
byte[] hkey = null;
for (String objectKey : objectKeys) {
... ...
package com.yoho.yhmessage.redis;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
/**
*
* PUSH专用redis,和公用redis集群分开
*
* @author hugufei
*/
@Service
public class PushRedisService {
@Resource(name = "pushRedisTemplate")
private RedisTemplate<String, String> pushRedisTemplate;
private ValueOperations<String, String> pushValueOperations;
private ListOperations<String, String> pushListOperations;
@PostConstruct
void init() {
pushValueOperations = pushRedisTemplate.opsForValue();
pushListOperations = pushRedisTemplate.opsForList();
}
/*************************** redisTemplate *****************************/
public RedisTemplate<String, String> getPushRedisTemplate() {
return pushRedisTemplate;
}
public void longExpire(String redisKey, long expiredTs, TimeUnit timeUnit) {
pushRedisTemplate.expire(redisKey, expiredTs, timeUnit);
}
/*************************** value_operation *****************************/
public boolean value_setIfAbsent(String redisKey, String string) {
return this.pushValueOperations.setIfAbsent(redisKey, string);
}
/*************************** list_operation *****************************/
public void list_rightPushAll(String redisKey, List<String> values) {
this.pushListOperations.rightPushAll(redisKey, values);
}
public List<String> list_range(String redisKey, long fromIndex, long toIndex) {
return this.pushListOperations.range(redisKey, fromIndex, toIndex);
}
public long list_size(String redisKey) {
return this.pushListOperations.size(redisKey);
}
}
... ...
package com.yoho.yhmessage.redis;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.yoho.core.redis.YHListOperations;
import com.yoho.core.redis.YHRedisTemplate;
/**
* yoho主从同步redis
* @author hugufei
*/
@Service
public class YohoRedisService {
@Resource(name = "yhRedisTemplate")
private YHRedisTemplate<String, String> yhRedisTemplate;
@Resource(name = "yhListOperations")
private YHListOperations<String, String> yhListOperations;
public long list_size(String redisKey) {
return yhListOperations.size(redisKey);
}
public List<String> list_range(String redisKey, int formIndex, int toIndex) {
return yhListOperations.range(redisKey, formIndex, toIndex);
}
}
... ...
package org.common;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yoho</groupId>
... ... @@ -25,6 +24,11 @@
<dependencies>
<dependency>
<groupId>com.yoho.dsf.yhmessagesender</groupId>
<artifactId>yoho-message-sender-common</artifactId>
<version>${project-version}</version>
</dependency>
<dependency>
<groupId>com.yoho.dsf.yhmessagesender</groupId>
<artifactId>yoho-message-sender-sms</artifactId>
<version>${project-version}</version>
</dependency>
... ... @@ -46,11 +50,14 @@
</dependencies>
</dependencyManagement>
<modules>
<module>common</module>
<module>dal</module>
<module>sms</module>
<module>inbox</module>
<module>push</module>
<module>web</module>
<module>deploy</module>
</modules>
</project>
<module>yoho-message-common</module>
<module>common</module>
</modules>
</project>
\ No newline at end of file
... ...
... ... @@ -8,22 +8,26 @@
<artifactId>yoho-message-sender</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.yoho.dsf.yhmessagesender</groupId>
<artifactId>yoho-message-sender-sms</artifactId>
<packaging>jar</packaging>
<name>yoho-message-sender-sms</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.yoho.dsf.yhmessagesender</groupId>
<artifactId>yoho-message-sender-common</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.dsf.yhmessagesender</groupId>
<artifactId>yoho-message-sender-dal</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.service.model</groupId>
<artifactId>message-service-model</artifactId>
</dependency>
<groupId>com.yoho.service.model</groupId>
<artifactId>message-service-model</artifactId>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-redis</artifactId>
... ... @@ -47,15 +51,15 @@
<version>20150729</version>
</dependency>
<dependency>
<groupId>com.cloopen</groupId>
<artifactId>CCP_REST_SMS_SDK_JAVA</artifactId>
<version>v2.6.3r</version>
<groupId>com.cloopen</groupId>
<artifactId>CCP_REST_SMS_SDK_JAVA</artifactId>
<version>v2.6.3r</version>
</dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-cache-clean</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>com.yoho.core</groupId>
<artifactId>yoho-core-cache-clean</artifactId>
</dependency>
<dependency>
<groupId>com.xuanwu</groupId>
<artifactId>postmsg-ump</artifactId>
<version>2.4</version>
... ...
... ... @@ -12,15 +12,13 @@ import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yoho.core.config.ConfigReader;
import com.yoho.yhmessage.sms.filter.IFilterByCountService;
import com.yoho.yhmessage.filter.IFilterByCountService;
import com.yoho.yhmessage.filter.IObjectKeyTransfer;
import com.yoho.yhmessage.sms.filter.IFilterUserService;
import com.yoho.yhmessage.sms.filter.IObjectKeyTransfer;
import com.yoho.yhmessage.sms.util.CommonUtils;
/**
... ... @@ -32,9 +30,8 @@ import com.yoho.yhmessage.sms.util.CommonUtils;
* @taskId <br>
* @CreateDate 2016年12月8日 <br>
*/
@Service("filterUserService")
@Service
public class FilterUserServiceImpl implements IFilterUserService {
private static final Logger log = LoggerFactory.getLogger(FilterUserServiceImpl.class);
private static final DateFormat df = new SimpleDateFormat("yyyyMMdd");
private static final String FREQUENT_USER_KEY_FORMAT = "msgcenter_sms_user_count_%s";
... ... @@ -42,24 +39,8 @@ public class FilterUserServiceImpl implements IFilterUserService {
@Resource(name = "core-config-reader")
private ConfigReader configReader;
@Autowired
private IFilterByCountService filterByCountService;
/**
*
* Description: 过滤场景<br>
*
* @author amos.shan<br>
* @taskId <br>
* @return <br>
*/
public List<String> getIgnoreMobileCountKeyList() {
String ignoreMobileCountKeyList = "";
if (StringUtils.isBlank(ignoreMobileCountKeyList)) {
return new ArrayList<String>();
}
return Arrays.asList(ignoreMobileCountKeyList.split(","));
}
private IObjectKeyTransfer<String> getMobileTransfer() {
return new IObjectKeyTransfer<String>() {
... ... @@ -95,6 +76,27 @@ public class FilterUserServiceImpl implements IFilterUserService {
/**
*
* Description: 过滤累计发送超过五次的用户<br>
*
* @author amos.shan<br>
* @taskId <br>
* @param mobileList
* @return <br>
*/
public List<String> ignoreFrequentUsers(List<String> mobileList) {
if (CollectionUtils.isEmpty(mobileList)) {
return mobileList;
}
String redisKey = String.format(FREQUENT_USER_KEY_FORMAT, df.format(new Date()));
int mobileSendLimit = this.getMobileSendLimit();
List<String> mobileWhiteList = this.getMobileWhiteList();
List<String> filtedResults = filterByCountService.batchFilter(redisKey, mobileList, mobileWhiteList, this.getMobileTransfer(), mobileSendLimit, 24 * 60 * 60,
TimeUnit.SECONDS);
return filtedResults;
}
/**
*
* Description: 过滤mobile白名单<br>
*
* @author amos.shan<br>
... ... @@ -111,34 +113,29 @@ public class FilterUserServiceImpl implements IFilterUserService {
/**
*
* Description: 过滤累计发送超过五次的用户<br>
* Description: 发送次数限制<br>
*
* @author amos.shan<br>
* @taskId <br>
* @param mobileList
* @return <br>
*/
public List<String> ignoreFrequentUsers(List<String> mobileList) {
if (CollectionUtils.isEmpty(mobileList)) {
return mobileList;
}
String redisKey = String.format(FREQUENT_USER_KEY_FORMAT, df.format(new Date()));
int mobileSendLimit = this.getMobileSendLimit();
List<String> mobileWhiteList = this.getMobileWhiteList();
List<String> filtedResults = filterByCountService.batchFilter(redisKey, mobileList, mobileWhiteList, this.getMobileTransfer(), mobileSendLimit, 24 * 60 * 60,
TimeUnit.SECONDS);
return filtedResults;
private int getMobileSendLimit() {
return configReader.getInt("msgcenter.sms.degrade.userSendLimit", 50);
}
/**
*
* Description: 发送次数限制<br>
* Description: 过滤场景<br>
*
* @author amos.shan<br>
* @taskId <br>
* @return <br>
*/
private int getMobileSendLimit() {
return configReader.getInt("msgcenter.sms.degrade.userSendLimit", 5);
public List<String> getIgnoreMobileCountKeyList() {
String ignoreMobileCountKeyList = "";
if (StringUtils.isBlank(ignoreMobileCountKeyList)) {
return new ArrayList<String>();
}
return Arrays.asList(ignoreMobileCountKeyList.split(","));
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- pool config -->
<bean id="pushJedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal:50}" />
<property name="maxIdle" value="${redis.pool.maxIdle:20}" />
<property name="minIdle" value="${redis.pool.minIdle:20}"/>
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis:2000}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow:false}" />
<property name="testWhileIdle" value="${redis.pool.testWhileIdle:false}"/>
</bean>
<bean id="pushRedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="pushJedisPoolConfig" />
<property name="port" value="${redis.push.port}" />
<property name="hostName" value="${redis.push.address}" />
<property name="password" value="${redis.push.auth}"/>
<property name="timeout" value="1000"></property>
</bean>
<bean id="pushRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="pushRedisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
</beans>
\ No newline at end of file
... ...
... ... @@ -4,7 +4,7 @@
#msgcenter.sms.degrade.ignoreUserCountKeyList=
#search open personalized[true:open personalized, false:close personalized]
msgcenter.sms.degrade.userSendLimit=5
msgcenter.sms.degrade.userSendLimit=50
#search.open.descorebrand[true:open descorebrand, false:close descorebrand]
msgcenter.sms.degrade.mobileWhiteList=18751886435
\ No newline at end of file
... ...