I have a web application
hosted by tomcat 7.1
using spring 3.1 mvc framework.
I want to implement a command line function for tomcat console window, to read commands and perform some action or output some information to console. How can I do it?
Example:
type "show memory", then display JVM memory information to console.
Can I do it just by System.in? Will there be any thread-safe problem?
If you're running as a web application in Tomcat, you won't get any access to System.in or the console: Tomcat is typically started in the background, detached from all consoles. And it won't provide you witn meaningful access to the console.
The way to go is to either provide a REST API as suggested by #rlegendi in the comments (any other API would work as well) and write a separate command line application that interfaces with your API. Alternatively utilize the "manager" interface - if I remember correctly from ancient past, tomcat's manager application also has some usable methods to access from external scripts.
Make sure to make those calls authenticated - at least validate that they're coming from localhost so that you simulate some kind of security in the API. Don't trust random calls coming in from the world.
Related
I know that by sending a http post request to http://host:port/shutdown, we can shutdown a Springboot application. Is it possible to restart the whole springboot application by sending a http request in a production environment? So we don't need to login in the server to do that. Thank you.
I don't think such a thing exists, I'll be glad to be proven otherwise:
Spring boot doesn't do any assumptions about the environment it runs in. So when spring boot process gets shut down, re-starting it again is "out of competence" of spring boot infrastructure which is just a bunch of java classes running inside a JVM process.
You can find Here a list of endpoints exposed by the spring boot. There is a "shutdown" method that you've mentioned there, but there is no "restart" functionality exposed.
Now there are other techniques that probably can help:
If the application gets shut down because of some illegal state of some spring bean, maybe it makes sense to expose some endpoint that will "clean up" the state and make application operational again. If the application has to be restarted due to changes in configuration files or something, then you might want to consider using spring cloud's Refresh Scope for Beans. It's kind of hard to provide more information here, because you haven't mentioned the reason for shutting down the application, but I guess you've got the direction.
Having said that, there are probably some different ways to achieve what you want depending on the environment your application runs in:
If you're running in AWS for example, you can take advantage of their autoscaling policies, shut down the application remotely and AWS will run another instance for you. I'm not an expert in AWS, but I saw this working in ECS for example.
If you're running "java -jar" just on some server and want to make sure that when your process ends (by using 'shutdown') it should be started again, its possible to use some kind of wrapper that would wrap the process in service and track the service availability. There are even ready solutions for this, like Tanuki wrapper (I'm not affiliated with this product but used once its free version and it served us great)
If you're using Docker infrastructure you can change the policy and restart the container automatically when it gets shut down, I haven't used this by myself, but according to This excellent blog post is perfectly doable.
You should look at Spring boot jenkins You will also find a small article explaining how to configure the project on jenkins.
I have the following problem. The customer have a Java EE web application running in EAServer 5.5.0 with a custom loggin. The loggin is very simple, when the application validates the credentials entered by the user through a web page, stores the user name and profile in a session variable. That way the application knows if the session is still alive. Now, the customer have another Java EE web application running in jboss server 7.1 and wants when the user log in the web application running on EAServer, at same time login in the application running in Jboss Server whitout any further steps.
My first idea was send the credentials via ajax call from EAServer to Jboss web application and stored the credentials in session, same way as the EAServer do. That seems to works fine on Internet Explorer, but in chrome or firefox the ajax call is done and the session variables are created in jboss, but when the user tries to see any view on the Jboss Application the session variables just gone.
Can somebody please tell me what is the best way or practice to do this? or what kind of problem I'm facing whit chrome and firefox that doesn't keep the session variables after ajax call?
One way you can do is the single sign-on that Leonardo Luiz suggested in the comment.
But I have another suggestion. Just install Redis and use it as a session store. This is very standard nowadays and it's broadly used in Ruby on Rails, NodeJs and Java as well.
Some libraries that might get you going:
Spring Session
Tomcat Redis Session Manager (I know you are using different servlet server, but still you can take a look to get the idea)
Or you might opt to use memcached. In that case you might use:
memcached-session-manager
The feature you need is called Single sign-on. I believe it's better to integrate an existing solution on your environment than create a custom one.
I already used two solutions for that:
http://picketlink.org/federation/ (Deprecated)
https://www.apereo.org/projects/cas
It will not be a ease task and requires some knowledge of security concerns.
If you really wanna to create a custom solution there is some information here: http://merbist.com/2012/04/04/building-and-implementing-a-single-sign-on-solution/
I have developed a simple application such as insertion, updation in jsp,servlet that is java web. What i want to do is: suppose i am inserting data into my oracle db then data is inserted and i got a message that data has been inserted successfully, same thing i want to show in a command prompt when my web app is running in tomcat server. Application will run in browser and simultaneously command prompt will show that :
this data is inserted into this table---transaction committed successfully----
How can i implement that? Any help is much appreciated
The vast majority of Java web applications use Log4j for logging.
To log messages to the server's console, use the Log4j ConsoleAppender class.
Normally you need to do little more than drop log4j.jar into your tomcat/lib directory, and configure logging with a log4j.xml or log4j.properties file, then have your servlets or jsps or controllers or services issue logging commands. There are many tutorials on the web that can get you started learning log4j with tomcat.
Oh and by the way, you can try plain old System.out.println() calls from a servlet, though this is only for learning. Professional applications should use a logger.
My java application uses swings and makes a connection to the MySQL database. I want to run this application as a windows service which should start immediately at the logon of any user.
I think Java Service Wrapper is useful only for console applications.So kindly suggest me a suitable method.
Thanks for the help!!!
You don't want to have apps with user interfaces as services. Services can start at boot, and if they pop up a panel, they can hang the service waiting for user input with no user to provide any input. Split out the UI from the service code if you want to run the code as a service. Or like Andy mentioned, place the exe in the users startup group.
If you want to create a Windows service, Exe4J makes this easy. It will wrap your Java application in an executable that supports options to install, start, stop and remove it from Windows services.
If you want to start a user interface at the start of a user's logon, you can put the application into the Startup group.
The binaries (e.g. tomcat6.exe, and tomcat6w.exe) that are included with Apache Tomcat can be used to turn a java application into a service. Here's a link to the documentation, which includes instructions for installing your app as a service, etc.: Tomcat as a Windows service
http://kenai.com/projects/winsw works great, and allows easy logging of System.out and System.err.
Used in Glassfish v3.
i am writing a standalone java app. the app's properties should be configurable from a webpage deployed with the app. how do i achieve this ?
Thanks in advance
note: the app has an embedded HTTP client/server module. it should only run from command prompt
I don't think that's a good idea. Webpage forms are designed to work with a server, not with a standalone client app. You could have the app run its own web server, but that would mean the app has to be running for the configuration page to work, and it's also a rather contrived setup just to do some configuration.
It might be possible for the webpage to contain JavaScript that writes to a local file - I don't know enough about the JavaScript security model to say.
But why not have the configuration dialog as part of the app's GUI? That's the normal and expected behaviour - you'd need a pretty compelling reason to deviate from it.
JMX might be the answer that you're looking for. If you expose all of your configurable properties through MBeans, then adding a web page on top of that exposing these properties is just configuration.
You can launch a standalone Java app using JNLP files (Java WebStart). If you want the user to be able to configure the application before its launched, you can have the JNLP file dynamically generated, then pass properties as environment variables through the JNLP file.
You can configure your standalone Java app to read configurable properties from a properties file (say conf.properties) on the server.
You may have a UI webpage (html/jsp) with all the field to be configured. When the page is submitted a JSP/Servlet may write/update the contents of conf.properties on the server.
UPDATE: The above solution will work assuming only an admin user wants to update the properties file.
In case anybody should be able to update it, then concurrency issue has to be taken into account.
In that scenario, you have to implement a mechanism similar to how weblogic10 updates config.xml using Admin Console.
i.e. You will have 2 conf.properties files confA & confB (initially in sync). The standalone app will always read from confB. The UI will have 2 buttons say Lock & Release configurations. When an edit is made (locked & released), it will be written to confA and at the same time changes of confA has to be replicated to confB.