在服务器上不断运行java应用程序

时间:2013-12-27 16:23:55

标签: java tomcat servlets

我希望在服务器上不断运行java应用程序。我有sevlets的经验,但他们不经常运行。通过研究我发现这个问题的常见解决方案是使用第三方定时服务;当我编写自己复杂的计时机制时,我对使用其中一个犹豫不决。

此应用程序需要全天候运行;我很困惑为什么人们不经常遇到这个问题 - 我做的事情是愚蠢的吗?

6 个答案:

答案 0 :(得分:1)

使用云主机。例如,您可以使用amazon aws创建一个免费的ec2实例。这些被称为微实例。然后你可以在机器上安装java并运行你想要的任何东西。

  1. 创建亚马逊aws帐户
  2. 创建新的EC2实例
  3. 选择操作系统(使用ubuntu / linux)
  4. 启动实例
  5. ssh进入EC2实例
  6. 安装java
  7. 做你想做的事。你基本上是一个全新的Linux计算机

答案 1 :(得分:1)

我强烈推荐OpenShift。它提供免费的云,支持Java和J2EE应用程序容器和服务器(tomcat,JBoss,Jetty ......)。 你可以在那里部署你的应用程序,并使用许多准备好的模块,如cron,maven,......你可以设置Jenkins ......

我将其用于开发目的近两年。

答案 2 :(得分:1)

问题似乎是“如何让我的程序全天候运行?”答案是:

1。)让您的计算机全天候运行并让程序运行。

2。)将你的程序放在像www.digitalocean.com这样的东西上,每月5美元,然后让程序运行。就像你的电脑一样。

William Falcon已经描述过,有很多云主机。

要进行扩展,只需将MyProgramThatRunsForever.jar复制到远程服务器,然后在后台运行,就像这样。它将运行并运行和运行,无论它做什么......

如果你问一个不同的问题,你会得到更好的答案。我个人不知道你为什么要这样做,并认为这是一个好主意。

请参阅:

  

X-Y问题,有时被称为,是一个心理障碍   这导致了大量的浪费时间和精力   寻求帮助的人,以及那些提供帮助的人。它   通常是这样的:

User wants to do X.
User doesn't know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y.
User doesn't know how to do Y either.
User asks for help with Y.
Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve.
After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn't even a suitable solution for X.

XyProblem

http://mywiki.wooledge.org/XyProblem

答案 3 :(得分:0)

最好的解决方案可能是重构需要不断运行到新应用程序的应用程序部分,从命令行(或cron)运行它并使用某种IPC与Servlet通信。

话虽如此,你可能正在做“愚蠢的事”。 Servlet建立在连接上,实际上整个Internet建立在连接的概念之上。我无法想象你需要运行的任何类型的网络相关任务,无论是否有连接。如果它是某种后台服务,它们通常与管理任务相关并作为shell脚本实现。如果必须使用Java,请参阅上面的段落,但不要尝试将其开发为Servlet并部署在servlet容器中。这绝对是错误的道路。

答案 4 :(得分:0)

从您的其他问题来看,您似乎已经知道如何执行此操作:

  

部署到AWS Elastic Beanstalk

     

Elastic Beanstalk服务器是添加的Eclipse WTP服务器   用于重新启动,发布和终止Java的功能   Web应用程序。 Eclipse中的Elastic Beanstalk服务器代表了一个   AWS Elastic Beanstalk环境。环境是一种运行   AWS平台上的应用程序实例。定义您的服务器

     

在部署应用程序之前,您必须定义服务器和   配置您的应用程序。

     

定义Java Web应用程序服务器

In Eclipse, right-click your Java web project in the Project Explorer view. Select Amazon Web Services, and click Deploy to AWS
     

Elastic Beanstalk。

In the Run On Server wizard, select Manually define a new server.

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.sdlc.html#create_deploy_Java.sdlc.create.deploy

看看这个样本:

   @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        getServletContext().log("init() called");
        count = 0;
    }

http://en.wikipedia.org/wiki/Java_Servlet#Example

将代码放入init方法,然后运行并运行并运行。类似的东西:

MyInfiniteLoopProject loop = new MyInfiniteLoopProject();
loop.countToInfinity();

然后,在您的MyInfiniteLoopProject课程中,添加以下方法:

public void countToInfinity(){
   int x=0;
   while(true){
        x++;
        System.out.println(x);
   }
}

并检查您的日志...

(好吧,取决于System.out去哪里,也许使用记录器)

答案 5 :(得分:0)

You can use Openshift cloud host. 

1. Create an Openshift account. https://openshift.redhat.com
2. Enable applications you need in your project. (JBOSS , Tomcat , MySQL etc)
3. Install Client tools to enable remote access to the cloud server. Follow steps in below wiki.
https://www.openshift.com/developers/rhc-client-tools-install

4. Follow this wiki to create public and private keys. https://www.openshift.com/developers/remote-access
5. Connect to your server and deploy the build.
相关问题