简体   繁体   English

在共享Web主机上连续运行Java应用

[英]Run Java app continuously on Shared Web host

I apologise if this is an obvious question, but lets say I had a java app that constantly queries yahoo finance API for current stock quotes. 如果这是一个明显的问题,我深表歉意,但是可以说我有一个Java应用程序,该应用程序不断向yahoo Finance API查询当前股票报价。 Could I store this program and have it run continuously on a shared web hosting server without being in constant communication with a client (my desktop)? 我可以存储此程序,并使其在共享的Web托管服务器上连续运行,而又不与客户端(我的桌面)保持持续通信吗? Would I need to install a JVM into my hosting space? 我需要在主机空间中安装JVM吗?

Along with this, would the app be able to create text and write to text files the same way as if it was operating on a desktop computer? 除此之外,该应用程序是否能够像在台式计算机上运行一样创建文本并写入文本文件? This way I could query the app once a day to download the text file and not worry about the program taking up my computer's RAM. 这样,我可以每天查询一次该应用程序以下载文本文件,而不必担心该程序占用了我计算机的RAM。

Finally, is there a way to determine how much RAM my app is using since my web host only offers x amount of RAM. 最后,由于我的Web主机仅提供x数量的RAM,因此有没有一种方法可以确定我的应用程序正在使用多少RAM。

If there is any literature you can supply that can give me some need to know general data on this subject, I'd be very grateful. 如果有任何您可以提供的文献可以使我有必要了解该主题的一般数据,我将不胜感激。

Could I store this program and have it run continuously on a shared web hosting server without being in constant communication with a client (my desktop)? 我可以存储此程序,并使其在共享的Web托管服务器上连续运行,而又不与客户端(我的桌面)保持持续通信吗?

If this it's a webapp and they're running something like Tomcat (I think AWS Elastic Beanstalk more or less does), then yes. 如果这是一个Web应用程序,并且他们正在运行类似Tomcat(我认为AWS Elastic Beanstalk或多或少都在运行),则可以。 Just make sure you flag a filter or a servlet as load-on-startup in the deployment descriptor (web.xml), and then scheduling API calls should be easy enough. 只需确保在部署描述符(web.xml)中将过滤器或servlet标记为load-on-startup ,然后计划API调用就应该足够容易了。 I'd start with ScheduledExecutorService for that. 我先从ScheduledExecutorService开始。 Java webapps, unlike how PHP, Python, and Perl are usually run, are more like applications in that code is alive for the life of the web server, not just the life of the request, so a single instance will serve some path for the lifetime of the webapp's deployment. Java Webapp与通常运行PHP,Python和Perl的方式不同,它更像是应用程序中的代码在Web服务器的生命周期中一直存在,而不仅仅是请求的生命周期,因此单个实​​例将为Webapp部署的生命周期。 Resources set up in init() (like threads) won't even be GCed until shutdown. init()设置的资源(例如线程)直到关闭都不会被GC。 Threads might not even be GCed (hint: don't ignore the destroy() method. 线程甚至可能没有被GC化(提示:不要忽略destroy()方法。

Would I need to install a JVM into my hosting space? 我需要在主机空间中安装JVM吗?

Oh, well, if you have "hosting space," you can do what you want, so why even worry about this being "web hosting" and not just rent-a-server? 哦,好吧,如果您有“托管空间”,那么您可以做您想做的事情,那么为什么还要担心这是“网络托管”而不仅仅是租用服务器? There actually are two flavors of hosting: one gives you a VM/physical machine, and you do what you want (think EC2), and the other gives you neutered access to a few scripting languages that start with P, Apache with cgi-bin, maybe modphp, and a managed MySQL instance that you have limited control over. 实际上有两种托管方式:一种为您提供虚拟机/物理机,您可以做自己想做的事情(想想EC2),另一种则使您可以绝妙地访问以P开头的几种脚本语言,Apache和cgi-bin ,可能是modphp以及您只能对其进行有限控制的托管MySQL实例。

Along with this, would the app be able to create text and write to text files the same way as if it was operating on a desktop computer? 除此之外,该应用程序是否能够像在台式计算机上运行一样创建文本并写入文本文件? This way I could query the app once a day to download the text file and not worry about the program taking up my computer's RAM. 这样,我可以每天查询一次该应用程序以下载文本文件,而不必担心该程序占用了我计算机的RAM。

Maybe. 也许。 Look into File.createTempFile() . 查看File.createTempFile() Or store your data in a DB and rematerialize it as a CSV, or something, when a request for /give-me-my-data comes in. But really, memory is so cheap, and this sort of data compresses so well I'm not sure I'd worry about it, yet. 或将您的数据存储在数据库中,并在请求/give-me-my-data时将其重新实现为CSV或其他形式。但是,实际上,内存是如此便宜,并且这种数据压缩得很好。我不确定我是否会为此担心。

Finally, is there a way to determine how much RAM my app is using since my web host only offers x amount of RAM. 最后,由于我的Web主机仅提供x数量的RAM,因此有没有一种方法可以确定我的应用程序正在使用多少RAM。

Runtime.getRuntime().*memory()

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

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