小弟最近开发CRM系统.因为在CRM中设立了一个功能,就是按用户需求定制备忘录.当此条备忘录信息到期就弹出类似QQ或者MSN的消息提示框,上面写到你有新短消息,请注意查收.
为此,小弟的设想是在后台写段定时器,在用户登陆时就启动,然后每5分钟检查一次有没有到期的备忘录,到期就触发类似QQ或者MSN的消息框.定时器如下
public class TimerListener {    public TimerListener() {
        
    }
    
    public void dolistener(){
        Timer timer = new Timer ();
        timer.schedule (new MyTask (), 0, 5*60*1000);
    }    static class MyTask extends java.util.TimerTask {        private Logger logger = Logger.getLogger (this.toString ());        public void run() {            System.out.println ("____TIMER____START");
            Getdate gd = new Getdate();
            String datetime1 = gd.getDateTime(); 
            compareDatetime cd = new compareDatetime();
            
            int b = 0;
            Session sessions = null;
            Transaction transaction = null;
            ConnDB connDB = new ConnDB ();
            try {
                sessions = connDB.getSession ();
                transaction = sessions.beginTransaction ();
                String sqlstr = "select co from com.greatking.ajcrm.hibernate.CustomerCaring as co where co.cflag ='2'";
                Query sqlquery = sessions.createQuery (sqlstr);
                List sqllist = (List) sqlquery.list ();
                
                if (!sqllist.isEmpty ()) {
                    for (int i = 0; i < sqllist.size (); i++) {
                        CustomerCaring customercaring = (CustomerCaring) sqllist.get (i);
                        String cdate = customercaring.getCcustomerdate();
                        b = cd.compare_date(cdate,datetime1);
                   
                        if(b==1){
                           //已经发生,备忘录到期
                            customercaring.setCflag("1");
                         customercaring.setUpdatedate(Calendar.getInstance().getTime());                   //此处填写AJAX FLAG---------------->
                        }
                                       }
                }
                sessions.flush ();
                transaction.commit ();
                sessions.close();
                //request.setAttribute("returnstr",cflag);
            }
            catch (Exception e) {
                logger.error (e);
                try {
                    if (transaction != null) {
                        transaction.rollback ();
                    }
                }
                catch (Exception ex) {
                    logger.error (ex);
                }            }
            finally {
                connDB.closeSession ();
                try {
                    if (sessions != null)
                        sessions.close ();
                }
                catch (Exception e) {
                    logger.error (e);
                }
            }
        }
    }}
当定时器启动后我如何能在前台任意页面下弹出消息框呢?最好无刷新的.
求达人熟悉AJAX的帮忙

解决方案 »

  1.   

    <script language =javascript>
       var titlePopup
       var len;
       
       function InitMsgBox()
       { 
       len = 0;
        titlePopup=window.createPopup();
        var titlePopupBody = titlePopup.document.body;
        titlePopupBody.style.border ="solid black 1px";
        var titleContent = "";
        titleContent = titleContent + "<table cellPadding='5' bgcolor='#65c1ff' width='100%' height='100%' border=0 cellspacing=0 cellpadding=0>";
        titleContent = titleContent + "<tr><td align=center><font size = 2>消息提醒</font></td></tr>";
        titleContent = titleContent + "<tr><td><a href='#' onclick=parent.winOpen('http://www.baidu.com')>百度</a></td></tr>";
        titleContent = titleContent + "<tr><td><font size = 2>内容1</td></font></tr>";
        titleContent = titleContent + "<tr><td><font size = 2>日期</td></font></tr>";
        titleContent = titleContent + "</table>";    
        titlePopupBody.innerHTML = titleContent;
             
        ShowMsgBox();
       }
       
       function winOpen(url) 

    window.open(url) 
    }    
          
       function MsgBox()
       { 
        len += 4;      
        if (len > 110)
        {   
         window.clearInterval(tID);     
        }        
        else
        {
         titlePopup.show(document.body.clientWidth - 170, document.body.clientHeight - len, 170, len, top.document.body); 
        }    
       }
     
       
       var tID
       function ShowMsgBox()
       {
        tID = window.setInterval("MsgBox()",15);    
       }
       
       
     
     </script>
     
     <body MS_POSITIONING="GridLayout">
      <INPUT onClick="InitMsgBox()" id="Button1"  style="Z-INDEX: 101; LEFT: 296px; POSITION: absolute; TOP: 344px" type="button"
       value="Button" name="Button1">
     </body>
      

  2.   

    同意bird,定时器放在前台,你的系统应该有一个主控制页面。