• java 泛型的使用


    参考文档

    http://www.cnblogs.com/panjun-Donet/archive/2008/09/27/1300609.html

    泛型的主要目标是提高 Java 程序的类型安全。通过知道使用泛型定义的变量的类型限制,编译器可以在一个高得多的程度上验证类型假设。

    可以查看下 Map, List , Future  等这些java 底层基础代码的写法,一目了然

    下面举个我应用中碰到的例子以及写法,之所以举这个例子,主要是因为它包含了多种情况,1 参数是范型,2 返回值是范型,3,参数和返回值都是范型

    app 产品中最不能缺的就是 push, 但是 ios 的推送 和 android 的推送还不一样,而且 android 推送还因为不同的第三方平台又不一样,比如小米推送,友盟推送,极光推送,百度云推送,个推推送等等

    所以总思路是写一个通用接口,以及一个抽象类(主要是实现发送push),因为发送push的流程都是一样的,见下面的代码

    接口

    package com.zyguo.tools.push;
    
    public interface IPush{
        /**
         * 异步发送PUSH
         */
        public boolean sendPushByAsyn( PushBody pushBody );
        
        /**
         * 同步发送PUSH
         * @return 返回错误提示文案,若为 success, 说明发送成功
         */
        public String sendPushBySync( PushBody pushBody, int maxWaitSec );
    }

    抽象类,因为不同的push通道,发送的消息格式是不一样的,而且返回值的格式也是不一样的(有json, xml ,byte 等等),所以定义了2个范型 M 代表消息,R代表返回值,private String sendPush(PushBody pushBody) {} 就是发送push的通用流程

    package com.zyguo.tools.push;
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.Future;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.TimeoutException;
    import org.apache.log4j.Logger;
    import com.zyguo.sdk.thread.BLThreadPool;
    import com.zyguo.sdk.utils.BUtils;
    
    public abstract class AbstractPush<M, R extends Object> implements IPush {
        protected Logger log = Logger.getLogger( this.getClass().getName() );
        public final static String SUCCESS = "success";
        private BLThreadPool threadPool = new BLThreadPool();
    
        
        private String sendPush(PushBody pushBody) {
            try {
                if ( pushBody != null && BUtils.isNotBlank( pushBody.getToken() ) ) {
                    long begin = System.currentTimeMillis();
                    log.debug("begin send push:" + pushBody );
                    M msg = pushBody2message( pushBody );
                    R response = sendPush( msg, pushBody.getToken() ); 
                    long end = System.currentTimeMillis();
                    log.debug("end push,result==" + response + ",耗时:" + ( end - begin ) + "毫秒");
                    if ( response != null ) {
                        String result = dealPushResponse( response, pushBody );
                        log.debug("send xiaomi push suceess," + pushBody );
                        return result;
                    } else {
                        log.error("no_response," + pushBody);
                        return "no_response";
                    }
                } else {
                    return "token is null";
                }
            } catch (Exception e) {
                log.error("send xiaomi push exception", e);
                return "exception:" + e.getMessage();
            }
            
        }
    
        
        protected abstract String dealPushResponse( R response, PushBody pushBody );
        protected abstract M pushBody2message( PushBody pushBody );
        protected abstract R sendPush( M msg, String token );
    
        @Override
        public boolean sendPushByAsyn( final PushBody pushBody) {
            threadPool.submit( new Runnable() {
                public void run() {
                    sendPush( pushBody );
                }
            });
            return true;
        }
        
        @Override
        public String sendPushBySync( final PushBody pushBody, int maxWaitSec ) {
            Future<String> future = threadPool.submit( new Callable<String>() {
                public String call() throws Exception {
                    return sendPush( pushBody );
                }
            });
            
            try {
                String result = future.get( maxWaitSec, TimeUnit.SECONDS );
                return result;
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            } 
            return null;
        }
    
        public void setThreadPool(BLThreadPool threadPool) {
            this.threadPool = threadPool;
        }
    
    
    }

    小米 push 如下, 具体实现就不写出了

    package com.zyguo.tools.push;
    
    import java.io.IOException;
    import org.json.simple.parser.ParseException;
    import com.xiaomi.push.sdk.ErrorCode;
    import com.xiaomi.xmpush.server.Constants;
    import com.xiaomi.xmpush.server.Message;
    import com.xiaomi.xmpush.server.Message.Builder;
    import com.xiaomi.xmpush.server.Result;
    import com.xiaomi.xmpush.server.Sender;
    
    public class XiaoMiPush extends AbstractPush<Message, Result>{
        private String appKey;
        private String packageName;
     
        @Override
        protected Message pushBody2message( PushBody pushBody ){
    return null;
        }
        
        @Override
        protected String dealPushResponse( Result response, PushBody pushBody ) {
    return SUCCESS;
        }
    
        @Override
        protected Result sendPush(Message msg, String token) {
    return null;
        }
    public void setAppKey(String appKey) { this.appKey = appKey; } public void setPackageName(String packageName) { this.packageName = packageName; } }

    iOS push 如下

    package com.zyguo.tools.push;
    
    import com.xiaomi.push.sdk.ErrorCode;
    import com.xiaomi.xmpush.server.Constants;
    import com.xiaomi.xmpush.server.Message;
    import com.xiaomi.xmpush.server.Result;
    import com.xiaomi.xmpush.server.Sender;
    import com.zyguo.sdk.utils.BUtils;
    
    public class IOSPush extends AbstractPush<Payload, Boolean>{
        @Override
        protected String dealPushResponse(Object response, PushBody pushBody) {
            return null;
        }
    
        @Override
        protected Payload pushBody2message(PushBody pushBody) {
            return null;
        }
    
        @Override
        protected boolean sendPush(Payload msg, String token) {
            return null;
        }
    }
  • 相关阅读:
    函数进阶-生成器
    函数进阶-列表生成式
    闭包
    命名空间
    内置方法
    函数
    squid清除缓存
    subprocess实现管道
    Python统计脚本行数(fileinput)
    fabric note
  • 原文地址:https://www.cnblogs.com/zyguo/p/4389568.html
Copyright © 2020-2023  润新知