-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lusifer
committed
May 26, 2017
1 parent
abf9e82
commit 2966f91
Showing
17 changed files
with
376 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
leesite-module/src/main/java/com/funtl/leesite/common/sms/SmsValidateDisruptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.funtl.leesite.common.sms; | ||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadFactory; | ||
|
||
import com.funtl.leesite.common.sms.consumer.SmsValidateEventHandler; | ||
import com.funtl.leesite.common.sms.publisher.SmsValidateEvent; | ||
import com.funtl.leesite.common.sms.publisher.SmsValidateEventFactory; | ||
import com.funtl.leesite.common.sms.publisher.SmsValidateEventPublisher; | ||
import com.lmax.disruptor.RingBuffer; | ||
import com.lmax.disruptor.YieldingWaitStrategy; | ||
import com.lmax.disruptor.dsl.Disruptor; | ||
import com.lmax.disruptor.dsl.ProducerType; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* 短信验证码的 Disruptor | ||
* Created by Lusifer on 2017/5/27. | ||
*/ | ||
public final class SmsValidateDisruptor { | ||
private static final Logger logger = LoggerFactory.getLogger(SmsValidateDisruptor.class); | ||
|
||
// 指定 ringBuffer 字节大小, 必须是 2 的倍数 | ||
private static final int BUFFER_SIZE = 1024; | ||
|
||
private ThreadFactory threadFactory; | ||
private SmsValidateEventFactory factory; | ||
private Disruptor<SmsValidateEvent> disruptor; | ||
private RingBuffer<SmsValidateEvent> ringBuffer; | ||
private SmsValidateEventPublisher publisher; | ||
|
||
private SmsValidateDisruptor() { | ||
// 执行器,用于构造消费者线程 | ||
threadFactory = Executors.defaultThreadFactory(); | ||
// 指定事件工厂 | ||
factory = new SmsValidateEventFactory(); | ||
|
||
// 多线程模式 | ||
disruptor = new Disruptor<SmsValidateEvent>(factory, BUFFER_SIZE, threadFactory, ProducerType.MULTI, new YieldingWaitStrategy()); | ||
// 设置事件业务处理器 -> 消费者 | ||
disruptor.handleEventsWith(new SmsValidateEventHandler()); | ||
// 启动 disruptor 线程 | ||
disruptor.start(); | ||
|
||
// 获取 ringBuffer 环,用于接取生产者生产的事件 | ||
ringBuffer = disruptor.getRingBuffer(); | ||
// 为 ringBuffer 指定事件生产者 | ||
publisher = new SmsValidateEventPublisher(ringBuffer); | ||
|
||
logger.debug("Created SmsValidateDisruptor."); | ||
} | ||
|
||
/** | ||
* 生产消息 | ||
* @param item | ||
*/ | ||
public void publish(SmsValidateEvent.Item item) { | ||
publisher.onData(item); | ||
} | ||
|
||
/** | ||
* 服务器关闭时别忘记调用 | ||
*/ | ||
public void shutdown() { | ||
disruptor.shutdown(); | ||
} | ||
|
||
/** | ||
* 静态初始化器,由 JVM 来保证线程安全 | ||
*/ | ||
private static class SingletonHolder { | ||
private static SmsValidateDisruptor instance = new SmsValidateDisruptor(); | ||
} | ||
|
||
public static SmsValidateDisruptor getInstance() { | ||
return SingletonHolder.instance; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...e-module/src/main/java/com/funtl/leesite/common/sms/consumer/SmsValidateEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.funtl.leesite.common.sms.consumer; | ||
|
||
import com.funtl.leesite.common.sms.publisher.SmsValidateEvent; | ||
import com.funtl.leesite.common.utils.CacheUtils; | ||
import com.lmax.disruptor.EventHandler; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* 短信验证码消息处理 | ||
* Created by Lusifer on 2017/5/27. | ||
*/ | ||
public class SmsValidateEventHandler implements EventHandler<SmsValidateEvent> { | ||
private static final Logger logger = LoggerFactory.getLogger(SmsValidateEventHandler.class); | ||
|
||
@Override | ||
public void onEvent(SmsValidateEvent event, long sequence, boolean endOfBatch) throws Exception { | ||
SmsValidateEvent.Item item = event.getItem(); | ||
String phoneNumber = item.getPhoneNumber(); | ||
String code = item.getCode(); | ||
String type = item.getType(); | ||
|
||
if (SmsValidateEvent.Item.TYPE_PUT_CACHE.equals(type)) { | ||
pubCache(phoneNumber, code); | ||
} else if (SmsValidateEvent.Item.TYPE_REMOVE_CACHE.equals(type)) { | ||
removeCache(phoneNumber, code); | ||
} | ||
} | ||
|
||
/** | ||
* 往缓存中放验证码 | ||
* @param phoneNumber | ||
* @param code | ||
*/ | ||
private void pubCache(String phoneNumber, String code) { | ||
logger.debug("生产短信验证码:手机号({}),验证码({})", phoneNumber, code); | ||
CacheUtils.put("smsCache", phoneNumber, code); | ||
} | ||
|
||
/** | ||
* 从缓存中删除验证码 | ||
* @param phoneNumber | ||
* @param code | ||
*/ | ||
private void removeCache(String phoneNumber, String code) { | ||
logger.debug("消费短信验证码:手机号({}),验证码({})", phoneNumber, code); | ||
CacheUtils.remove("smsCache", phoneNumber); | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
.../src/main/java/com/funtl/leesite/common/sms/consumer/SmsValidateEventPutCacheHandler.java
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
...c/main/java/com/funtl/leesite/common/sms/consumer/SmsValidateEventRemoveCacheHandler.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.