ブロックキュー送信メッセージ


メッセージ送信サービスは、メッセージを送信する値がブロックキューにputメッセージ情報を送信する必要がある
 
SmsServer.putTask(new LendSms(map));

 
 
public class SmsServer {

    private static final Logger log = LoggerFactory.getLogger(ProTask.class);

    private static final ExecutorService executorService = Executors.newFixedThreadPool(5);

    private static final BlockingQueue taskQueue = new ArrayBlockingQueue(10000);

    private static WebApplicationContext webApplicationContext;

    private static Client client;

    public static void initSmsServer(WebApplicationContext webApplicationContext) {

        SmsServer.webApplicationContext = webApplicationContext;

        /**
         *  
         * */
        String softwareSerialNo = PropertiesUtil.getValue("sms.softwareSerialNo").toString();
        String key = PropertiesUtil.getValue("sms.key").toString();
        client = SingletonSmsClient.INSTANCE.getClient(softwareSerialNo,key);

        /**
         *  
         * */
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (;;) {
                    try {
                        List taskList = Lists.newArrayList();
                        taskList.add(SmsServer.taskQueue.take());
                        taskQueue.drainTo(taskList,19);
                        executorService.submit(new ProTask(SmsServer.webApplicationContext,taskList));
                    } catch (InterruptedException e) {
                        log.error("sms server error:",e);
                    }
                }
            }
        }).start();

     }


    public static void putMsg(SmsMessageEntity smsMessageEntity) throws InterruptedException {
        taskQueue.put(smsMessageEntity);
    }

    public static void putTask(AbstractMessagePro abstractMessagePro) throws InterruptedException {
        taskQueue.put(abstractMessagePro);
    }

    public static SmsMessageEntity sendMessage(SmsMessageEntity smsMessageEntity) throws RemoteException {
        int status = client.sendSMS(new String[]{smsMessageEntity.getMobile()},smsMessageEntity.getMessage(), "", 5);
        smsMessageEntity.setStatus(status==0?0:1);
        smsMessageEntity.setFirmStatus(status);
        return smsMessageEntity;
    }

 
メッセージ抽象クラス
public abstract class AbstractMessagePro {

    protected WebApplicationContext webApplicationContext;

    protected Object object;

    protected abstract List<SmsMessageEntity> toMessages();

    protected void setWebApplicationContext(WebApplicationContext webApplicationContext){
        this.webApplicationContext = webApplicationContext;
    }

}

 
メッセージ実行task
public class ProTask extends Thread {

    private static final Logger log = LoggerFactory.getLogger(ProTask.class);

    private WebApplicationContext webApplicationContext;

    private List taskList;

    public ProTask(WebApplicationContext webApplicationContext,List taskList){
        this.taskList = taskList;
        this.webApplicationContext = webApplicationContext;
    }

    @Override
    public void run(){
        ISmsMessageDao iSmsMessageDao = (ISmsMessageDao) webApplicationContext.getBean("iSmsMessageDao");
        List<SmsMessageEntity> taskResult = Lists.newArrayList();
        for (Object o:taskList) {
            if(o instanceof SmsMessageEntity) {
                SmsMessageEntity smsMessageEntity = (SmsMessageEntity)o;
                taskResult.add(smsMessageEntity);
            }else{
                AbstractMessagePro abstractMessagePro = (AbstractMessagePro)o;
                abstractMessagePro.setWebApplicationContext(webApplicationContext);
                List temp = abstractMessagePro.toMessages();
                taskResult.addAll(temp);
            }
        }

        List<SmsMessageEntity> results = Lists.newArrayListWithCapacity(taskResult.size());
        for (SmsMessageEntity smsMessageEntity:taskResult) {
            try {
                log.info("send message to {},content {}",new String[]{smsMessageEntity.getMobile(),smsMessageEntity.getMessage()});
                smsMessageEntity = SmsServer.sendMessage(smsMessageEntity);
            } catch (RemoteException e) {
                log.error("task error",e);
            }finally {
                smsMessageEntity.setSmsMessageNo(StringUtil.getUUID());
                results.add(smsMessageEntity);
            }
        }
        iSmsMessageDao.inserList(results);
    }
}

 
リスニング開始smsserver
public class SystemInitializedListener extends ContextLoaderListener {

    private static final Logger log = LoggerFactory.getLogger(SystemInitializedListener.class);

    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext servletContext = event.getServletContext();
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        SmsServer.initSmsServer(webApplicationContext);
    }

}