Share instance between Java App and Web App - java

I have some personal project where I am trying to share some class instance between webapp and a normal app. My project structure is something like below:
+ NormalApp
+ WebApp
I am starting the application from the NormalApp and I included WebApp using EmbeddedTomcat. Now I have a class named Notifier in WebApp. I want to use the instance of Notifier in NormalApp without losing it's state.
Could someone tell me how can I achieve this scenario?
I have some plan in mind like setting the Tomcat class loader to use Systems class loader. I tried it but couldn't able to achieve it. Is my understanding of this wrong?

Have you tried making your normal app like web components ? so your application will be available when tomcat start. And you can use System properties to pass parameters to the tomcat.
Another option is using Spring boot. Here is a tutorial
https://spring.io/guides/gs/spring-boot/
http://spring.io/guides/tutorials/bookmarks/

Related

Use log4j on a deployed war

I've made a trivial RESTful service on a JBoss server that just says "hello" when it receives any request. This part works and it is already deployed.
To go further with my project I need to log. The other applications on the server use log4j configured by a log4j.xml placed on a specific folder on the server.
For an offline project I'm used to have a runnable main method that in this case I would use to execute DOMConfigurator.configure(filepath) (where filepath is the log4j.xml) and I will be expecting to log with those settings.
However, since this is a deployed service (and since I'm not skilled enough to figure it myself) how would I so such a thing?
The question in my opinion could be interpreted in two ways:
How do I run some code "for sure" when I deploy something (in similar way to a main method) ? Do i need something like spring-boot?
How do I setup log4j on a deployed service on JBoss?
(I don't know if one question excludes the other...)
Thanks for the help!
1) If you want to run some code "for sure" you can create #PostConstruct method in one of your beans. Both Spring and EJB support it.
2)As for log4J configuration it should be enough to put it in classpath and add corresponding dependencies, no explicit configuration of path should be needed.

Jersey 2.5: How can a resource access external classes instances

I have embedded Jetty container inside my main server and I also use Jersey 2.5 for handling REST resources.
everything seems to work well and now I would like to pass my server's context class into each of my REST resources.
I'm having hard time understanding how to do that...
If someone can provide full code example - it could be awesome!
Many thanks
What exactly do you mean when you say you have a Jetty container inside your "main server"? Are you programmatically executing Jetty within the application? Also, when you say "context" are you referring to the ServletContext?

Spring Shared bean concept - per App Server

I have a portal server. There are number of portlets installed. I have environment configs file which is same for all portlets installed on that app server. I am using apache commons-configuration for managing from the configuration files.
Right now, each portlet/component build their own EnvConfiguration bean from these files. Obviously this is eating up more memory. I want to separate out the creation of EnvConfiguration bean and share it across all the portlets. Basically, I want to create only one bean per app server.
What are the best possible ways to do this? And how will this common beans(s) injected into the needed components?
Edit: This is a liferay application and non EAR model
Thanks
Sundar
You can use the parentContextKey web.xml param.
Here is a blog post explaining how : http://spring.io/blog/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/
The idea is to package and declare the beans you want to share in a common jar (if you use Tomcat, you can put it in the server lib folder). The beans are declared in a file called beanRefContext.xml (default name, can be changed), with an id. Then you add the param "parentContextKey" in the web.xml of your webapps with the id previously mentionned.

Provider oracle.j2ee.ws.client.ServiceFactoryImpl not found

I am writing an application that invokes an Oracle web service. The web service client code was provided me.
This application uses a custom subclass of URLClassLoader to load jars at run-time. Running the unit tests from my local machine works fine, however when I deploy the application on the server and run it, I get the following error. Other classes/jars are loading fine through the custom ClassLoader.
Not sure why this is happening? Maybe FactoryFinder is using a separate ClassLoader instance? Any help is appreciated. Thanks!
javax.xml.rpc.ServiceException: Provider oracle.j2ee.ws.client.ServiceFactoryImpl not found
at javax.xml.rpc.FactoryFinder.newInstance(FactoryFinder.java:44)
at javax.xml.rpc.FactoryFinder.find(FactoryFinder.java:137)
at javax.xml.rpc.ServiceFactory.newInstance(ServiceFactory.java:69)
at com.mycomp.myapp.oracle.client.TSClient.<init>(TSClient.java:34)
at com.mycomp.myapp.oracle.LaborHours.update(LaborHours.java:92)
at com.mycomp.myapp.oracle.OracleConnection.updateMetrics(OracleConnection.java:73)
at com.mycomp.myapp.Project.crawl(Project.java:150)
at com.mycomp.myapp.Main.main(Main.java:302)
This is the conclusion I came up with.
ServiceFactory uses it's own class loading mechanism (which apparently is a different instance than my custom class loader).
I had to use this hack http://forums.sun.com/thread.jspa?threadID=300557 to add the jars to the system class loader's class path.

Start / stop a web application from itself?

I've made a web application using Java, Struts and running over Apache Server and Tomcat. It would be very useful to be able to restart the application from the web. I don't want to restart Tomcat, only this webapp, the same way as Tomcat Manager does it.
Do you know how can I do it? If not, any way to simulate that behaviour (reload config.properties, make Hibernate init process, etc)?
Thank you a lot.
I took a quick look at the source code for the Tomcat Manager. It looks like there's a class that's part of the Tomcat source called "Container Servlet". From the javadocs:
A ContainerServlet is a servlet
that has access to Catalina internal
functionality, and is loaded from the
Catalina class loader instead of the
web application class loader.
A ContainerServlet automatically gets passed a wrapper that can be used to get the Context and Deployer -- and the Deployer has helpful methods such as start(String contextPath) and stop(String contextPath) that will do what you want.
So, what I think you would need to do is write your own servlet that inherits from ContainerServlet, and configure Tomcat to load your servlet using the Catalina class loader (look at how the Manager is configured to see how). Note that this is probably not going to be an option for you in a hosted environment.
Then your servlet could have, say, a button you press to reload the application. I recommend putting password-protection of some kind in front of that. :)
Just hit the URLs
http://<username>:<password>#<hostname>:<port>/manager/stop?path=/<context path>
to stop and
http://<username>:<password>#<hostname>:<port>/manager/start?path=/<context path>
to start. That simulates you using the manager application.
Tomcat Manager offers an http interface to start/stop an application and other tasks. There are Ant tasks that you can use to easily access these operations.

Categories