简体   繁体   English

如何在启动Apache tomcat服务器而不是使用ScheduledExecutorService的Java类时定期运行javascript函数?

[英]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. 我需要在服务器启动时定期运行我的javascript函数。 i dont know how to write the equivalent Java code as my javaScript function contains multiple ajax call and mathematical equations. 我不知道如何编写等效的Java代码,因为我的javaScript函数包含多个ajax调用和数学方程式。

below is my javascript code which i need to run periodically on server start. 以下是我的JavaScript代码,我需要在服务器启动时定期运行。

<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? 有什么技术可以从通过runnables扩展的Java类中调用javascript函数,或者直接在java类中实现确切的javascript吗?
  2. If answer for 1 is no, can you please guide me to convert my entire javascript to Java code? 如果1的答案为否,您能否指导我将整个JavaScript转换为Java代码?

Any piece of code is highly appreciated and thanks in advance. 任何代码都将受到高度赞赏,并在此先感谢。

Use node.js and execute your script using Runtime.exec(). 使用node.js并使用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. 您可以在此节点上运行节点,也可以使用Rhino脚本引擎从Java类中的.js文件中调用方法。 Hope that helped. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java ScheduledExecutorService定期运行 - Java ScheduledExecutorService to run periodically 使用ScheduledExecutorService方法定期运行一批任务 - Using ScheduledExecutorService method to run a batch of tasks periodically 如何使用ScheduledExecutorService通过多个服务定期运行作业 - How to use ScheduledExecutorService to run the job periodically with multiple services 如何使用apache或tomcat服务器在docker中运行java应用程序 - How can i run java applications in docker using apache or tomcat server 如何使用其他名称而不是localhost运行tomcat服务器 - how to run tomcat server using with different name instead of localhost 使用ScheduledExecutorService在Java中定期运行任务 - Using a ScheduledExecutorService to run a task on a periodic basis in Java 定期运行ScheduledExecutorService或根据条件运行一次 - Run a ScheduledExecutorService periodically or just once based on condition 如何配置Maven在启动tomcat服务器后运行测试? - How to configure Maven to run tests after it starts tomcat server? 在Tomcat服务器上部署Java servlet时如何运行某些代码? - How to run some code when java servlet is deployed on Tomcat server? 在后台运行Java ScheduledExecutorService - Run Java ScheduledExecutorService in the background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM