简体   繁体   中英

How to run javascript function periodically when the Apache tomcat server is starts instead of java class using ScheduledExecutorService?

I need to run my javascript function periodically on server starts. i dont know how to write the equivalent Java code as my javaScript function contains multiple ajax call and mathematical equations.

below is my javascript code which i need to run periodically on server start.

<script type="text/javascript">




$(document).ready(function(){


var polyLat = new Array();
polyLat[0] = 10.194027;
polyLat[1] = 10.226975;
polyLat[2] = 10.059987;
polyLat[3] = 10.002248;
polyLat[4] = 9.854925;
polyLat[5] = 9.835443;
polyLat[6] = 9.899107;
polyLat[7] = 9.993088;
polyLat[8] = 10.081425;
polyLat[9] = 9.992266;
polyLat[10] = 10.194027;//First point repeated to close polygon
var polySides = (polyLat.length)-1;//number of points in polygon
//vertical Longitude coordinates of polygon 
var polyLng =  new Array();
polyLng[0] = 76.201205;
polyLng[1] = 76.375022;
polyLng[2] = 76.775730;
polyLng[3] = 76.778940;
polyLng[4] = 76.584336;
polyLng[5] = 76.411473;
polyLng[6] = 76.368070;
polyLng[7] = 76.397007;
polyLng[8] = 76.317492;
polyLng[9] = 76.267905;
polyLng[10] = 76.201205;//First point repeated to close polygon
//Coordinates for bounding box
var maxLat = Math.max.apply(null,polyLat);  
var minLat = Math.min.apply(null,polyLat);
var maxLng = Math.max.apply(null,polyLng);
var minLng = Math.min.apply(null,polyLng);


$.post('outboundupd.jsp',
        {
    mx_lat:maxLat,
    mn_lat:minLat,
    mx_lng:maxLng,
    mn_lng:minLng,
    ply_sds:polySides
        },
        function(response,status,xhr)
        {
//          alert(response.trim());
            plotdata(response);


});

    function plotdata(response)
    {
        var x;
        var y;
        var mob;
        var jsonArray=JSON.parse(response.trim());
        var jalen= jsonArray.length; 
        for(i=0;i<jalen;i++)
        {
            var obj=jsonArray[i];
            pcode= obj.Pcode;
            nplate= obj.N_plate;
            driver= obj.Driver;
            mob= obj.MobileNu;
            x= obj.Latitude;
            y= obj.Longitude;
            time= obj.Time;

        }


        var j = polySides-1 ;
          oddNodes = 0;
          for (i=0; i<polySides; i++) {
            if (polyLng[i]<y && polyLng[j]>=y  ||  polyLng[j]<y && polyLng[i]>=y) {
                if (polyLat[i]+(y-polyLng[i])/(polyLng[j]-polyLng[i])*(polyLat[j]-polyLat[i])<x)  {
                    oddNodes=!oddNodes; 
                }
            }
           j=i; }




            if(oddNodes!=true)
            {
//              alert("ob mobile:"+mob);

                $.post('obsouth.jsp',
                        {

                    pcd:pcode,
                    npt:nplate,
                    drv:driver,
                    mobl:mob,
                    lat:x,
                    lon:y,
                    tm:time

                        },
                        function(response,status,xhr)
                        {
                            alert(response.trim());


                });

            }

          return oddNodes;


        }

});

</script> 

My question:-

  1. Is there any technique to call a javascript function from a java class extended with runnables or directly implement the exact javascript in java class?
  2. If answer for 1 is no, can you please guide me to convert my entire javascript to Java code?

Any piece of code is highly appreciated and thanks in advance.

Use node.js and execute your script using Runtime.exec(). you would run node on this or you can go for Rhino scripting engine to envoke methods in .js file from within a java class. Hope that helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM