main class of a tomcat web application - java

I have a client server application. The server is made of restful services with jersey and is deployed on tomcat 7. Actually, I need to create the context of the services (read some high sized files) before the client access to the services. Is it possible to create a main class of my webapp or not?

A web application in JavaEE doesn't have a "main class" in the same sense that a desktop application does; surely, the execution must start on a main method somewhere, but it'll be managed by the web container (Tomcat in your case) and outside of your reach.
What you can do instead, is create a servlet which preloads the data you need in the application context using its init method (assuming that the data will be the same for all the clients, and ideally, it won't be modified by them). Also, in the servlet configuration, you specify that the servlet must be loaded on startup, and in that way you make sure that the data will be loaded once at the beginning of the application, and that all the clients will be able to access it from the application context.
EDIT :
In more recent versions of the Servlet specification (2.3+) the preferred way is to use context listeners, see this answer for details.

Related

How to get list of loaded application classes in Tomcat?

Let's say that I have an application deployed to Tomcat. Ideally, I want to be able to undeploy said application without stopping my server and all classes that belong to the application should be cleanly unloaded from Tomcat.
But, it may happen that I've created some object somewhere that's preventing all classes from being unloaded. Is there a mechanism to list loaded classes so I can check if the application is being unloaded (undeployed) cleanly or not?
I'm aware of Tomcat's JMX interface but I haven't found any of the information I'm looking for in that.
I want to be able to undeploy said application without stopping my
server ...
Use the Admin Console also known as Tomcat Web Application Manager. It does exactly what you need: Stops particular application, unloads all its classes. Open URL your_host:port/manager/html.

Can an EJB access methods of standalone java application?

I have a standalone java application that initializes and establishes socket connections, both server and client. The standalone java application have operations such as startConnection, stopConnection, getConnectionStatus, etc.
I would like to develop EJB to access or invoke the standalone java application operations, such as getConnectionStatus. The EJB will be deployed to Glassfish.
If the EJB can access the java application and receive results, would you provide an example, references, and/or implementation strategies?
I am not quite sure what you are trying to do.
You can either create some EJB(s) and include the classes that your application uses in your .jar file, or package that application's jar into your EJB's (.ear or .war) file (depending on how you are deploying). This would statically link the code which runs with a "standalone" application with your EJB(s).
Alternatively, you can add some remote invocation methods to your standalone application and turn it into a server that can accept commands. To do that, you can look at Hessian which can allow you to remote services in your application. Here is one example that I found: https://karussell.wordpress.com/2009/04/10/hessian-web-service-protocol-hello-world-example/

How to access existing application context running in tomcat in external main class

My web application is running in tomcat 6. We are using DOJO for UI and web services for data access.
Currently, there is a webservice available to clear/build the cache in our application.
As we are access this using a https/http, it is holding huge memory and taking much time to process.
so currently we are planning to take this process out of web services and wanted to execute as a standalone app.
I need to have a standalone (backend) application (may be main class) which should do the above activity.
The problem is that all beans used to clear/build the cache are available in application context (meaning inside the tomcat container).
I want to access the same available beans in external main class and do the process.. (possible ?)
How to get the application context outside the tomcat scope, I mean in external java class ?
Else, can i have another application (jar file with one main class file) which i will deploy it with the same application and trigger it via tomcat (possible?), so that the application context is available for the main class thereby we can access the beans/context.
Earlier in my previous project, we have used EJB home and remote interface to connect to main class (jar file with only one main class containing EJB connecting code) deployed along with the application.
But the applciation server is WEBSPHERE.
can we do the same thing in tomcat.
kindly help on this... thanks in advance for your replies...
I think it is not possible, JAVA manages its own memory, you are not allowed to manipulate the memory via another application.

Does EJB container reside in all application servers. EJB container in depth

I am newbie to EJB's. From all the reading and searching I have done till now, I understood the following:
EJB are the beans in which an applications business logic is written and maintained.
All EJB's are put into something called EJB container.
EJB container is nothing but a server side program written in order to manage EJB's, and to provide basic functionalities which are meant to be provided by EJB(viz, transaction management, security, collision free envt, etc).
1) My doubt is, does the so called EJB component reside in all application servers?
2) When we say EJB 2.1/3.0/3.1, does it mean that the new version of EJB container has been released?
3) Does the EJB container reside in web servers too?
Thank you.
You understood the EJB idea correctly.
Yes and no. Depends on what you understand as "Application server" (ambiguity described below in answer 3.)
When you say EJB 2.x/3.0/3.1 or so on, you're referring to a particular EJB specification which means that you're referring to a set of services this version supports. In other words - yes, it means that the EJB container must be in a given version.
First the specification is released (you can see the draft versions, vote for new features and basically participate in this process). Then, a reference implementation (RI) is written just to show that it's "doable" and you can use it right away. Then, different vendors might provide their own EJB containers which must conform to the particular EJB specification.
There are few different terms you need to be aware of. Just to be sure, we're talking about the same things:
Web server is a HTTP/HTTPS server like Apache HTTP Server which serves clients requests. This term is not only related with Java EE.
Web container is a Java EE term which can mean few things, but usually it refers to Servlet container and, let's say JSP container. Those containers are serving web clients, so that's why it's web container. Generally, web container have a web server within it (like in the case of Tomcat.) However, you can configure it so that the static resources will be server by only pure web server while dynamic content (your Java App, Servlets, JSP, etc.) will be server by your web container.
Application server is a vague name. In Java EE purists world it can mean only such server that provides all the Java EE services. Non-Java EE purists treats Application server just as an arbitrary server which consists of your application. According to this definition, you can call Tomcat (a web container and web server) an application server.
As you see, the vocabulary is not sharp, as one thing might mean few slightly different things. Moreover, since Java EE 6 we have profiles. This means that you can have Java EE Application Server conforming to the Web Profile or Full Profile. In such terms, just the Web Profile server should be treated as an application server.
Just as a summary - you can use EJB Container in Web Container. Take a look at OpenEJB or basically at project TomEE.
To answer your questions
Generally yes. Application server is generally referred to a server with has EJB container like Glassfish, Jboss etc. But you need make sure that the application server has EJB support.
YES
NO. Web servers or Web containers (Tomcat, Jetty etc ) serve a different purpose than EJB container. But all the application servers do have web servers (along with EJB containers. ).
The EJB container and web container (servers) server are different layers in a Java EE application scenario . Check this link for more info.

Java application inside tomcat?

I have a standalone java application which uses java based TCP NIO to collect some information from various clients (not on web/HTTP but through some indigenously developed middleware). Now I have to develop a front-end for the users to perform a lot of querying through HTTP. So is there a way to put this application inside tomcat, so that servlets can invoke required functions on this application ???
The thing is it has to listen on that middleware outside tomcat too and at the same time service servlets inside tomcat. How to do this ??
Putting this application outside tomcat and using RMI is an option but I don’t want to do that.
Can embedding tomcat inside my application is an option ???
The brand-new Tomcat 7 has Embedded version for download.
Tomcat is big. You should try to embed Jetty, which is meant in order to make it possible.
http://jetty.codehaus.org/jetty/
I don't know about embedding Tomcat, but you can embed a servlet container inside your application using embedded Jetty. If what you want is to add an HTTP interface to an existing server, I think that's the way to go.
It also should be perfectly workable to launch your server's TCP listening components from a standard servlet in Tomcat. Then the servlet can call methods in your existing application directly, while it continues to listen to its usual TCP ports.
A third option is to write a servlet that just talks to your existing server in the same way other clients do.
The way you want to go, is to have an embedded web server to your existing application since you just need a little bit of web functionality.
Going the other way is possible. You can use servlet listeners to get started and stopped, and you need to take great care of any thread you start since Tomcat have no idea they exist.
As Traroth said, embedding Jetty is a much better option than doing the same with Tomcat. You have an example and a small tutorial in the Jetty website:
http://docs.codehaus.org/display/JETTY/Embedding+Jetty
You can always open the ports and listen manually if the requests aren't complicated.
Have you considered going the other way? That is embedding your application inside a web application?
Using spring you can instantiate your web application and then inject in into the appropriate web classes (e.g. struts2 actions).
This way you can deploy your web application into any web server that is preconfigured separate from your application.
If you do have to use Tomcat, you're best just making your application as a .war file which when Tomcat is ran will deploy your code. This can then talk to any RMI/middleware you like.
I'm not sure I understand your comment regarding memory spaces.
At a very basic level you have two choices:
Run your appication and the web
application in the same virtual
machine.
Run your application and
the web application in a different
virtual machine
If you take approach 1, you will be able to get instances of your applications classes from the servlets and call methods on them.
Embed tomcat / jetty inside your application. You will start your application normally through its main method. After starting your app you will need to create instances of the tomcat / jetty classes which will start up their own threads.
Embed your application inside a war file to be deployed on tomcat / jetty. Similar to the previous option instead you will need to start your application from a context listener.
If you prefer approach 2; start tomcat / jetty and deploy your web app and separately deploy your application. You'll now have two jvms running. To communicate between these two processes you'll have to use some form of socket communication: OutputStream/InputStream over sockets; RMI; JMX; JMS etc.

Categories