Can a .jar be run as a webservice?
Since jar file consume a lot of cpu on my development machine, I would like to call and run it on another machine via network. Need advice on this, what is the best way of doing it?
Thanks.
You can create a web service to run a Java command line application. It is relatively straightforward, but you will need to code a servlet wrapper that (typically):
maps HTTP requests and their arguments onto calls to your application, and
turns the output from the application into something that a web browser can handle.
And there are significant limits on what the service will be able. For instance, it won't be able to read or write files into the local file system of your development machine.
can you use JVM and use something like tomcat or some other java container?
lots of docs out there for that kind of stuff, most containers even have their own samples on how to create webapps.
Related
One my customers has very strict requirements about how they can deploy a web application. They require it to be deploy-able as WAR within a Java server like Tomcat. Our application however is written in golang and compiles into an executable server.
The only solution I could think off would be to start a long lived golang process in the background which listens on a non-standard port like 8080 and then put some kind of a proxy in java which would transparently proxy all HTTP requests and responses to this process.
How should I go about implementing this ? I'm not at all familiar with Java Servlets and running long running processes like this in the background.
My main concern is if something like this is standard,
would it affect the JVM's memory usage, etc ?
Would the server allow my golang process to allocate the required
amount of memory ?
Would the JVM be able to track it's processor utilization ?
Is there a better way to do this maybe ? Like some kind of inter-process communication mechanism ?
If HTTP is the only API your Go application exposes, then you're only left with the option to just send a HTTP request to Go. Yes you can start the Go executable as a child process from the JVM using a ProcessBuilder. But that's not truly embedding go within your WAR. You can use JNI (Go bindings are called GoJVM) to invoke native routines in Go from Java, but then you have to make a lot of adjustments to your Go code base as well.
What you are looking probably for is indeed a reverse proxy. It's not frequently done in servlets / application servers, because this is usually done in load balancers already. So usually that would be NGINX or httpd with mod_rewrite. For application servers, you can use a proxy servlet.
Examples:
https://github.com/mitre/HTTP-Proxy-Servlet
https://sourceforge.net/projects/j2ep/
The memory footprint is usually not to bad, because you're essentially only connecting the streams. The JVM will be able to track processor and memory utilisation - if implemented properly and the Go executable runs on the same server. Obviously the Java specific statistics don't make much sense anymore, as you're only using your Java server as a pass-through.
Sounds like you will be in trouble though. If my strict requirement was a WAR-archive that can be deployed to an application server, I wouldn't accept another executable added to my stack. Reverse proxied or not.
If they want war, give them war!
Write a thin layer web service that has the executable embedded as a resource that executes it to bring up the go server and accepts web calls and does a pass through to the go service.
Achieves nothing I hear you say? Well, neither does their arbitrary requirement that was probably put in place by someone who's long since left the company, and/or because they lack the confidence/experience/ knowledge to operationally deal with other forms of deployment. There is no particularly good reason to deny your service to be deployed except the work required to establish proper operational procedures (laziness).
Just follow the letter of their rules, not the spirit, and be honest if asked about your implementation. They won't mind.
This may sound like a rant, but IMHO and experience, this kind of approach is reality and the drivers for the requirement are emotionally based, so a somewhat emotional answer is warranted.
I have a jar file which contains two Java classes. Using the javamail API I have developed these classes to read and edit my mail, then send to another mail id.
I am able to execute this through my standalone system via Eclipse. Now I want to host this jar file somewhere remotely so that it would fetch the data in real time and execute the job regularly. I have contacted couple of hosting sites and they are saying that they require a war file instead.
Does anyone have any suggestions to this problem?
To give you another point of view and to be constructive, I would go with embedding your jar into a war application and you get some things for free, the most important I think is that you gain a managed application lifecycle so with a standard web application context listener you can start and stop your program in a managed way. Besides you have more hosting options and it is less work.
Good luck with that.
As I don't know of any services specifically for plain execution of executables, your best bet is probably getting a cheap VPS. With some searching you can probably find one that would work for around $5 USD/month. For a single simple app you'd only need around 128MB of memory.
Pick one up, install Java (whatever Linux distro you get probably has OpenJDK in the repositories), copy your files over, and set up a cron job to run the executable at a set interval.
For easier administration, install something like webmin and use that to configure the cron job. The command would likely just be java -jar /path/to/my/App.jar, and you can use the web interface to configure the intervals for the command to be executed.
For an app like this, I would avoid anything related to a war file. You aren't making an application with a web interface (like a PHP app or some such) so it really wouldn't be appropriate. You would have to write some extra code to make it compatible with a container like Tomcat, and on top of that the memory requirements for running the application server would be a lot higher.
I have a problem relating my web project.
Previously when my professor ask me on doing a online judge application, I chose to implement it using php rather than Java servlet, as I thought java servlet quite complicated.
Then problem comes. For the online judge, I have to use a backend java program on the server to do the processing of user submitted codes. That is, each time a user submits something, php would call the java virtual machine and invokes the java application. However, to invoke the java application, my own way now is to use command line
popen("start java -jar \"$FILE_ROOT/OnlineJudge.jar\"", "r");
This works fine, but considering loading of java virtual machine, it is actually very slow and error prone. So I was wondering if there is any better ways for PHP to invoke local java programs on the server. Because later I found I still need to invoke more java from php.
Any idea would be appreciated. Thanks.
have you considered php-java bridge, this is quite useful, it uses XML to communicate between php and java.
You'll need to start the JVM once and the bridge component will help with communication.
here are some examples, here you can find a simple example to connect php and java
Depending on how the java application is written, it likely wouldn't be too hard to convert it to a servlet. Once it is a servlet, run it under jetty or tomcat, and then just have your php connect to it via CURL and put/get data from the servlet.
Java servlets are fairly simple. If you are using an external framework like Spring it becomes even easier, but if not, you just need to extend javax.servlet.http.HttpServlet, and then configure it in your web.xml.
It's a bit of a hack, but shouldn't take you too long to accomplish.
Also take a look at the exec for command line executions. I used it to shut down my computer after doing an extensively long task well into the night.
I need to develop a game server that will run periodically (e.g., triggered by a CRON job every five minutes or hour as appropriate). Once started up, the server will access all of the current game state (fetched through REST from the game's data servers (Stackmob, Parse or similar), do the processing of player actions, and then POST the results back to the data server. In other words, it will be doing a lot of HTTP requests, but does not itself necessarily need to be a web service.
I've been considering multiple ways of developing this.
I do not feel for setting up a server myself, so I need to find a service to run this on that permits the workflow I would like.
The game engine is Java, so something that works neatly with that.
Will need to GET and POST data files, so access to static files would be needed.
Most of the services that exist which provide something similar to what I require are directed at web services - which generally means that one needs to jump through some hoops to get things to work.
Google App Engine, for instance, would require that I implement this using backends (since the game server could potentially run for more than 60 seconds), and isn't particularly happy with the idea of static files.
Amazon EC2 would seem easier to develop on (again by building a web service frontend, of course), but there seems to be relatively poor support for CRON.
Generally speaking, it feels like I want to shoot some sparrows with a slingshot, but all the services are offering me cannons. Are there any alternative platforms/frameworks beyond the big two mentioned above that would be suitable for something like this?
You could try Heroku. They support Java. If you created a project that used a single worker dyno then the hosting would be free (see link).
The process would be running continuously, so you might want use a Timer for periodic execution. You could also use Quartz, but it might be overkill.
Edit:
Here's some links that might help get started:
Running non-web Java processes on Heroku
Heroku Java quickstart - this is for a web app ('web dyno') rather than a 'worker dyno', but it may help.
java.herokuapp.com has links to some example projects (again web apps rather than workers)
How about using EC2, but rather than putting the scheduler in the instance (which won't work because the instance can go away at any time), putting it in AWS? Like this guy:
http://alestic.com/2011/11/ec2-schedule-instance
Alternatively, if you manage your EC2 instances through Ylastic, it looks even easier:
http://blog.ylastic.com/scheduling-tasks-on-the-aws-cloud
Although you'll have to pay for Ylastic as well as EC2, i imagine.
I found a nifty way of writing something like this in Groovy with Maven. You can write a multithreaded Groovy script to pull the stats, do the updates, etc. and then have maven's assembly plugin assemble the whole thing into a self-contained, executable jar file that can be called by a CRON job. One nice thing about Groovy is that its syntax allows you to do this:
def google = "http://google.com".toURL().text
which will turn the string into a URL and handle all of the details of turning the URL into a HTTPURLConnection and getting the raw text.
You could develop the app as a standalone Java program first and then worry about where to deploy it later.
To develop the app you could write a simple Java program that uses HtmlUnit to talk to the external web services. The job could be internally started via Quartz. If you really wanted to start the job externally via CRON, you could have CRON run the app, passing in args. The app would then run and exit.
Alternatively, you could have the app always running and have cron run a bash script that triggers the job in some way.
Essentially, all you need to deploy is a Unix machine so you could use AWS.
I need to start a copy of a Rails app from within Java. I would favor a lightweight HTTP server, as our installations will have a very small userbase (1-10, 10 being a huge installation).
My design I am aiming for is for a single process, with the web interface written in Rails - running on JRuby in a background thread of the main server written in Java.
Any tips on starting up Rails in this way? I very much don't want a separate Tomcat server running.
Thanks!
You could just create war files for every installation (google for "warbler") and serve them through one tomcat, or use jetty for each installation (which can be a little more lightweight than tomcat, depending on your configuration).
As far as I know, you can even run script/server via jruby (which starts webrick through jruby).
The simplest way that springs to mind is to make a shell call like
jruby script/server
... from you Java app.