Android之百度云推送(三)如何启动服务端的定时任务

Android之百度云推送(三)如何启动服务端的定时任务

apk和服务端都好了,怎么能定时启动服务呢?
查看了好多资料:
spring task定时器的运用
http://blog.csdn.net/xwygn/article/details/8440692
定时任务实现Timer, TimeTask, ScheduledExecutorService及Spring定时器
http://blog.csdn.net/majinggogogo/article/details/49962081

无耐公司spring版本是2.5的不支持

@Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次  

升级spring试试,原先的项目报错,不值当的改,改动太大了

一共这么几种定时任务,都不行,怎么办

还是自己玩吧,我选择的是线程,

web项目

第一步写个类启动一个线程,并且希望这个类随web服务器启动而启动

PushJob.java

@Component("PushJob")
public class PushJob  extends HttpServlet {  
    private static final long serialVersionUID = 1L;
      @Override
    public void init() throws ServletException {
        // TODO Auto-generated method stub
          System.out.println("开始 pushjob init .....");
        super.init();
        PushMsg p = new PushMsg();
        Thread t = new Thread(p);
        t.start();
        System.out.println("完成 pushjob init .....");
    }
} 

web.xml进行相应的配置随web服务器启动而启动

    <servlet>
        <servlet-name>PushJob</servlet-name>
        <servlet-class>
            com.ailk.app.push.servlet.PushJob
        </servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>

第二步修改AndroidPushBatchUniMsg.java为PushMsg.java

听着很奇怪是吧?看我的

public class PushMsg implements Runnable{
    @Override
    public void run() {
        System.out.println("PushMsg running....");
        while(true){
            doone();
            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    private void doone(){
        //原先AndroidPushBatchUniMsg.java的main函数修改
        // 1. get apiKey and secretKey from developer console
        String apiKey = "WnK9iAq465461313GORSyWC";
        String secretKey = "MTVkTFfdsaf443Rgb29hF8d";
        PushKeyPair pair = new PushKeyPair(apiKey, secretKey);
        // 2. build a BaidupushClient object to access released interfaces
        BaiduPushClient pushClient = new BaiduPushClient(pair,
                BaiduPushConstants.CHANNEL_REST_URL);

        // 3. register a YunLogHandler to get detail interacting information
        // in this request.
        pushClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
                System.out.println(event.getMessage());
            } 
        });

        try {
            System.out.println("开始了吗");
            //从数据库获取消息通知,这个是我自己服务获取的方式,你们改改这就行
            PushBean pb = new PushBean();
            List<PushInfo> pushList=pb.queryPushList(ac);

            System.out.println("有信息吗?"+pushList.size());
            if(pushList.size() > 0){

            for (PushInfo pushBean : pushList)
            {
                // 4. specify request arguments
                //创建Android通知
                JSONObject notification = new JSONObject();
                notification.put("title", pushBean.getTitle());
                notification.put("description",pushBean.getDescs());
                notification.put("notification_builder_id", 0);
                notification.put("notification_basic_style", 4);
                notification.put("open_type", 3);
                JSONObject jsonCustormCont = new JSONObject();
                //自定义内容,key-value

//              jsonCustormCont.put("logStaffCode", "zq");  
//              jsonCustormCont.put("logStaffId", "982985123456");  
//              jsonCustormCont.put("logManagerId", "422493");  
//              jsonCustormCont.put("managerTypeId", "49");  
//              jsonCustormCont.put("logLatnId", "-1");  
//              jsonCustormCont.put("logAreaId", "1");  
//              jsonCustormCont.put("H5Url", "http://www.baidu.com");  
                notification.put("custom_content", jsonCustormCont);                
                String[] channelIds = { "3898756644656267390" };
                PushBatchUniMsgRequest request = new PushBatchUniMsgRequest()
                        .addChannelIds(channelIds)
                        .addMsgExpires(new Integer(3600))
                        .addMessageType(1)
                        .addMessage(notification.toString())
                        .addDeviceType(3)
                        .addTopicId(pushBean.getTopicId());// 设置类别主题            
                // 5. http request
                PushBatchUniMsgResponse response = pushClient.pushBatchUniMsg(request);

                // Http请求结果解析打印
                System.out.println(String.format("msgId: %s, sendTime: %d",response.getMsgId(), response.getSendTime()));

            }

            }

        } catch (PushClientException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                try {
                    throw e;
                } catch (PushClientException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } else {
                e.printStackTrace();
            }
        } catch (PushServerException e) {
            if (BaiduPushConstants.ERROROPTTYPE) {
                try {
                    throw e;
                } catch (PushServerException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            } else {
                System.out.println(String.format(
                        "requestId: %d, errorCode: %d, errorMessage: %s",
                        e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我想你应该看出来了,其他的都没动,就是你怎么去数据库拿数据了吧。。不懂得可以加群玩android不是梦 261403514咱们商讨一下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值