I designed my java spring application to run a couple of python programs on same server and communicate with them. I run them with ProcessBuilder and communicate via InputStream/OutputStream.
Now I want to achieve that when I restart or shut down my java application, python apps didnt close. I can't get Process object by PID. With ProcessHandler object I can't get input/output streams. It seems that i should use some other mechanism of IPC. So the questions are:
How can I run external applications from java so they wouldn't close when java app restarts?
How can I achieve communication between java and python applications without having Process object?
Thanks in advance, sorry for poor language :)
I give some suggestion on our project experience:
In single server, use docker-compose to run/stop app no matter using java or python. In multi-server,they will be deployed in Istio.
use Restful protocol to communicate between them.
Related
I want a piece of java code to run on a particular port.
for exampple localhost:007 will return {key: value}
should i write a socket server in java, or should I use Tomcat?
The beautiful thing about Java is that there are so many existing libraries and frameworks out there that there's almost always an existing tool or framework to solve your problem. It will save you a heap of time to use Tomcat or some other lightweight container like Jetty for this task.
Alternatively, if this is a learning exercise then writing your own simple web server sounds like good fun!
I was wondering if it's possible to send commands from a website to a java program being ran on a computer.
Basically what I'm doing is creating a robot, but I want control over it when I'm away from my computer. So what I was thinking, was that if I could send it commands (Like 'Stop' or 'Start') from a website, I could use my smartphone to control it.
If you know a way that might work or another method that's similar please let me know, thanks!
What I've done in the past is built the Java robot into a Java EE webapp, then deployed the webapp on Tomcat. Tomcat is a Java-based web server. It's a web server, but there's also no reason you can't run arbitrary code inside it, like a robot.
Another alternative is to embed a web server into the robot, and have the robot serve up pages itself. An example of an embedded HTTP server is JETTY.
Using the above two approaches, the web pages and Robot can communicate with each other directly through Java code. It's a single process and a single JVM running both.
A third alternative is to connect the Java robot process with the web server process via sockets or another form of IPC. This could be tricky, but decouples nicely.
one starting point is:
RPC the wikipedia article will guide you through other options and names that can help you. If it is really that basic. Socket Programming is the right way.
I was wondering if it's possible to send commands from a website to a
java program being ran on a computer.
This equivalent to asking if 2 programs can communicate via network.
The answer is yes. It is obvious, right? I mean how do you connect to your website in the first place? Eh?
When starting a application, one can pass parameters to the application. But how can one pass parameters to a already running (Java) application / how can I handle such cases in my Java program?
In other words: How can I communicate from a .bat file / CLI processes with a already running Java application? Note that both things (CLI stuff and Java application) are my own applications and I can adapt the source code - I just don't know how ;-)
I prefer using socket for cross platform IPC, with help of Apache thrift . You can implement RPC method to use from CLI utility.
I work on a standalone Java application that is a command-and-control system for an assortment of hardware. The C&C software basically runs from a command line, and controls the system hardware which is spread out all over the place. It does not require a GUI to meet the business requirements. I have written a small swing GUI just so I can see what the overall status is of the system, but again, that is not essential.
Going forward, we would like to have a Administrative web GUI with system status and something that would give a user some level of control over the hardware. We were thinking that GWT might be a viable solution. Our GWT app would have to have some sort of IPC with the C&C software. I don't know how viable that is, I don't know that we want the C&C software bundled as a web app that has to run under application server.
I just don't have much experience with this. I was thinking that the GWT client would interact through RPC with the GWT server, and the GWT server would have some sort of RPC (RMI???) with the C&C.
Another option you have is to run a Java Applet that can interact with both GWT (using JSNI) and with the OS. A simple example would be to open a common file in the user's home folder and read/write to that file (with the C&C app doing the same as well).
You can embed embedded tomcat or Jetty into your application and let it run a simple servlet/JSP - based or GWT-based web UI. In fact, when you debug/run your GWT application from Eclipse, it starts in an embedded Tomcat instance.
I found a solution by using Spring. We have modified much of our Command and Control app to use Spring IoC, and then we used other Spring libraries to extend its functionality. To interact with GWT, we exposed services and used httpInvokers to make calls from the GWT server code.
Of course, this meant we needed to use Spring with our GWT application too in order to make the http calls from the GWT server code. But all is working.
you dont have direct control on the client system with JavaScript (which GWT is based on). in your use-case that wouldn't even make sense, why using an external server for a website controlling a local program?
If you reverse this (the app server is running on the same system as the C&C software, and the web client is available from everywhere) than that would be possible, but that wouldn't have much to do with GWT. On the server you can write normal java code. GWT just would be used for the web GUI and the RPC-calls to the server.
The call to the C&C software from the app server could be realised with the following line (windows example):
Runtime.getRuntime().exec("C:\\PathTo\\Program.exe")
This function return a instance of class Process which provides an Input- and OutputStream to simulate user input and to read and process the programs output.
Please note that you lose platform independence with this method, because the parameter for exec() looks different for every OS.
EDIT
After re-reading your question, it would even make more sense to integrate the C&C software into the server code directly, as the comment on your question suggests. you need a application server to use GWT-RPC, not a webserver, but thats hairsplitting.
Informations on how GWT-RPC generally works can be found here: (tutorial), (detailed description)
I am new to java and want to develop a java application which will run continuously like a server.
Is it possible in Java to develop a UI less application which will work continuously? This application should also have JNI support, so functions exported using JNI should get called from a C++ application.
Can anybody tell me the pointers to start?
Help is appreciated, thanks.
Vishal N
It is certainly possible to run without a UI. By the sounds of it you would like to create your Java APP as a console app and then run it as a Windows Service/Linux Daemon (You did not mention the OS). There are java service wrappers out there that lets you run your java code as a service (e.g. this one) or you could write your own.
I recommend this book to learn the JNI. Although it sounds like what you need it not really JNI but rather an interface exposed over TCP or something similar that another C++ app can use to talk to your app. JNI won't allow another process to talk to your app, it is there to extend your Java code with functionality that cannot be implemented in Java itself e.g. calling some Windows API function.
Edit:
By the way, a plain Java Hello World App like this one IS a console app:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
Yes you can develop UI less applications in Java.
I'll expect an application server to handle multiple requests, transactions, security, life-cycle, persistence, etc.
If answer is Yes then I will choose the Java EE route to implement your requirements. I'll implement my business model using EJB3.0 and deploy in one of the application servers i.e. Glassfish, JBoss etc. which will support all features described above without reinventing the wheel.
Note: The solution will also give flexibility to expose your remote methods using WebService, CORBA or JMS.
If answer is No then I might create my own standalone server type application which will listen on some port and communicate through some bespoke protocol.
In order to support JNI -I would expect that you'd have to write a plain JavaBean wrapper or proxy. This proxy would then be used by the JNI.
Yes.
Is it possible in Java to develop a UI less application which will work continuously?
Create a thread or code main thread that will run foreever.
This application should also have JNI support, so functions exported using JNI should get called from a C++ application.
yes , have a look at here
You can certainly develop a Java application without a user interface; I think most folks would agree that it's easier than developing one with a user interface.
Regarding JNI, if I understand the question, you've got it backwards; using C++ as an example, JNI lets you call C++ code from Java, not Java code from C++.
Yes, you can run Java as service (daemon). You can simply run your code a "daemonize" it. This way, it will be detached from terminal sessions and will run in background.
Take a look here:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo022
to see how you can run service inside C.
And here, to check out how to run service part in Java:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo029
Have fun with JNI!