Call java application launched from another container - java

Here's situation. I have one container with php code and php-fpm - this is my application container. Sometimes main application calls java application - jar file. So I decided to split those technologies and make seperate java container with this java application. Now I need a way to call jar file launched inside another container. One way is rebuilding java applicaton to support REST api, but it takes time so is there any other possibility to solve this problem?

You could take a look at Java Remote Method Invocation (Java RMI), but to be honest, I have no idea if it's possible to to this invocation not using java.
https://docs.oracle.com/javase/8/docs/technotes/guides/rmi/hello/hello-world.html

Related

working as servlet but not as java class. why?

I am working on SAML SSO Authentication.
I have created a servlet to generate SAML metadata and i deployed it and run it and I got the output.
Same time I have created a java class to generate SAML Metadata with the same code and tried to run it independently. I have added the same Jar files that I have used for that servlet application.
But I got the Exception given below. Can anybody help me to find the difference between running an application independently and by using java servlet??
Thanks in advance.
Exception:
Running as servlet in a web container means all sorts of stuff is on the classpath that is automatically provided by the servlet container.
Running using main() means you have to put all needed stuff on the classpath yourself. The ClassNotFoundException you got should be clear enough in that respect.
(Pls note that although I did say "the" classpath, in a servlet container things are typically not quite that simple. But that's not the point. Also note that running as a servlet, and using features of libraries provided for the container, may even mean your stuff cannot run as an independent java program simply because the library stuff was deliberately intended for servlet container use exclusively.)

Call from java Main class from servlet Application

I Have two applications.One is Standalone application.Another one is web Application (Servlet App).
Here I want to Call Java Application From Servlet Application.So can You Guys Suggest Me or Can i have one app for this one.So that i will implement in my application.
In this Application I have created Jar file of Stand alone Application After that What to do ?
First, you should make your command line application classes available in your web applcaction environment. To achieve this you should either:
package all in one jar
put classes of stand alone application under WEB-INF/classes in you war
put jar that contains your stand alone application under WEB-INF/lib in you war
Stand alone application starts from main() method. You can just call this method from your servlet and pass parameters, for example: MyApp.main("hello", "stand alone application").
Since you are the author of both applications and are familiar with the internal design of the stand alone application you could (and probably should) call internal layer directly without using main(). For example if you main method starts with new MyApp().start() perform the same call from your servlet.
Just Switch from GlassFish to Tomcat , And then your problem has been solved.

How to build a command line interface in java for an existing web based application

I have created a web based application using JSP and Servlets and the application uses an SQL Server DB as its backend.
The architecture is like this:
I have all my business logic in a jar file
I have created my views using JSPs and am using servlets to interact with my business logic jar
The jar connects to the database to persist and hydrate information, which is relayed to the JSP by my servlets.
My web application runs on a remote Tomcat server.
Now, I have been given a new requirement. I have to create a command line interface, where I should be able to specify a list of commands and hit enter (or alternatively, create a set of commands and save it in a .bat file or something, and run it), so that my application performs the necessary actions. Basically, I have to create a command line interface, which can be used along with the GUI i already have (JSPs).
I am totally new to this. Can anyone throw light on where and how I can start?
Any little help is greatly appreciated.
EDIT
This is what my web application does. User can see a list of test scripts (written in Selenium WebDriver). He can choose script(s), choose a host on where to run them from, and click "Run", and the test executes on the said machines.
Now, I want a command line interface, which will eliminate the need for the GUI. Let's say, I simply want the user to be able to type a command like "execute My_Script_1", and the script should be executed.
The test scripts, the selenium drivers, everything reside on the App server.
My command line interface should be able to work on Windows command prompt.
Thank you.
Are you using Spring?
Can you specify, what exactly your CLI should do?
You may do, what Thomas said.
You also may use template engines like Velocity.. To form your output.
Use some kind of JavaCurses-like library to make your output... Look well.
Specifying commands...
Hm.. think about your business logic what exactly you are showing to user.
Remember webapp ui is webapp ui. Console ui is different. And user expects different behaviour
So commands like
show goods category="for kids"
Will be great.
Also don't forget about different help commands
yourJarName.jar --help / -h and etc
If your are want to write application with interactive mode... think about help command there.
You say you have your business logic in a JAR.
Why not starting another project with this JAR as a dependency and build it as an executable jar ?
Then simply use System.in and System.out to interact with the user.
EDIT :
So your application is hosted. Do you have an API like REST or SOAP or any other ?
Then you can build a client reading a string that the user has written, parsing it and calling the right service in your API.
I see two options:
Create a client-side CLI that generates the same data your server
receives. In other words, you don't modify your server code, and you create a
client-side CLI module (with jQuery for example) that parses the command lines and sends
exactly the same thing your actual GUI sends.
Set up a text area in your web app (decorated as a CLI) that reacts
on each Enter key pressed, and sends the line(s) to your server. On
your server, you can create a utility class (say CLIParser.java for
instance), and use Args4j to parse the received command,
validate it and run it.
Have you looked at Primefaces terminal? http://www.primefaces.org/showcase/ui/misc/terminal.xhtml
You data structure looks simple enough. Also you mentioned you designed your application the way the business logic is separated from the front end.
In this case you may consider exposing your business logic as a REST based WebService. It should not be that hard since you have layered structure in your application.
Looks like a few methods:
list scripts - returns a list of available scripts list hosts
returns a list of available hosts run script(scriptName, hostAddress)
runs script scriptName on a host with on address hostAddress possibly returns the results if your application supports this
All three look like a good candidates for GET methods.
You may consider Jersey or Resteasy or another framework.
You can find plenty tutorials for both of them. Take a look for example here.
From your command line application you can make calls to your web service in different ways. Just because I used to work with Jersey JAX-RS implementation most of the time, I found use of Jersey client(the latest stable version) the most convenient. Here you can find a short tutorial how you can do it from your command line application with Jersey client. JBoss also has a client API as a part of their framework(also fully certified JAX-RS implementation). You may even decide not to use any client API and do all the work manually utilizing HttpURLConnection, but I would not recommend. There is no big difference in using client API or do all the work manually with HttpURLConnection for the simple service, but you never know when your application becomes not that simple because of new requirements your client could not think of at the beginning.
Hope that helps

How do I make a custom windows service?

Actually I am having one java program. That returns "Hello World". I converted it to jar file. After that I changed the file type as .exe with the use of iexpress in my windows XP default sw.
I used the sc create command, for creating new service. I created the windows service successfully. But the thing is I am not able to run this. It is throwing the following error.
This error is coming not only for this javaservice.
I tried to run the chrome.exe as windows service, That time also it is throwing the same error.
Is it windows constraints? or Am I making mistake? Expecting the solution.,
Thanks in Advance...
You cannot just assign any arbitrary .exe file as-is to run as a service. There are specific API functions involved that services must use to interact with the Service Control Manager (SCM). That is why you are getting the errors - those .exe files are not using those APIs to interact with the SCM.
If you are not writing code specifically for SCM interaction (Java does not support creating Windows services), then all is not lost. In some situations (Chrome NOT being one of them!), you can use a separate wrapper to host non-service apps and handle the SCM interactions on their behalf:
Service wrapper
For Java apps, there are a few wrapper projects available:
Java Service Wrapper
Yet Another Java Service Wrapper
ow2

Executing a class in remote jvm

I have a small test class that I want to run on a particular jvm that's already up and running (basically it's an web application running on Tomcat) . The reason I want to do this is I want to execute a small test class (with the main method and all) within that jvm so that I get the same environment (loaded and initialized classes) for my test class.
Is it possible to indicate that ,say through a jvm parameter, that it should not initialize a new vm to execute my class but instead go and execute on the remote vm and show me the result here, on my console. So the local jvm acts as a kind of thin proxy ?
I am not aware in case there are some tools that should make this possible .Also heard somewhere that java 6 jvm comes with an option like this , is that true ?
Please help me.
Thanks,
After reading this question and the answers, I decided to roll my own little utility: remoteJunit
It is lightweight and dynamically loads classes from the client to the server JVM. It uses HTTP for communication.
You might want to take a look at btrace. It allows you to run code in an already started JVM provided you don't change the state of the variables inside that JVM. With this kind of tracing, you might be able solve your problem in a different way. Not by running extra code in form of a new class but by adding safe code to and existing class running inside a JVM.
For instance, you might System.out.println the name of the file when there is a call to File.exists.
You might find JMX useful. Register an MBean in the server process. Invoke it with visualvm (or jconsole). (tutorial) Never tried it myself, mind.
RMI would also do the magic.
http://java.sun.com/javase/6/docs/technotes/guides/rmi/index.html
Make your web application start an RMI registry and register your service
beans there.
Then in other JVM you can run a program that queries the RMI registry
started by your web application for the services you want to verify
and you are done.
I assume "small test class" is basically some debugging code you want to run to monitor your real application, which is deployed remotely on a Tomcat. If this is the case, you should connect your Eclipse debugger remotely to the Tomcat instance, so you can set a breakpoint at interesting locations and then use the Display view of Eclipse to run any arbitrary code you might need to perform advanced debugging code. As java supports Hot Code Replacement using the debug mechanism, you can also change existing code on the remote side with new code at runtime.

Categories

Resources