• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Java application inside tomcat?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ???
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have no problems running the non-GUI classes that make up the application as part of a web app in Tomcat. A servlet context listener would be a good place to start the related background threads, open server sockets, etc.
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be possible to embed the Tomcat web server inside your application and then deploy your web component inside the application. However, this would be an unnecessary and overly complex solution. Especially since there are many easier and simple solutions available. You would need access to someone with strong "distributed" programming skills, e.g. JMS, web services, EJB/JNDI, etc.



 
xbhanu Choudhary
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But the application will have its own memory space.
From what i've heard is that once you deploy a war in tomcat it becomes a process, and my app is also a process. So even though i may use the same classes but they will be two different processes and all the data that i want to query on lies in the application's address space. !!! And there is no GUI involved. Its all core java with some logging.
Feel free to correct me if i am wrong.
 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what you're trying to say, but what I proposed would run the relevant classes as part of the web app - there would be one JVM process that handles both the web app and the socket server. Do you have a concern that this might not work? If so, what is it?
 
xbhanu Choudhary
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay forget everything, see it like this -

i have a desktop java application, it contains two maps which are updated at every 60 second interval. These needs to be exposed to whoever wants them through HTTP. So i am gonna need a web server for that say tomcat.
My application becomes a model, i'll use JSP's for the viewer and servlets will be controller. So i am using struts to implement this.

Now, normally you see servlets picking up data from a database or struts action files doing something. I have to get it from this java application. Now how to make this connection b/w tomcat and my standalone java application is the question ???
 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd rewrite it so that it's part of the web app. If you followed best practices in your standalone app and separated GUI code from networking code, then it should be relatively easy to rip out the networking part and integrate it into the web app.
 
xbhanu Choudhary
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as luck would have it, there's no GUI code in the app. It just computes. For the GUI part only, i need to combine it with the server, so that user/ops can just type in the url and see the stats and do querying etc. So, you're saying that the app can run inside tomcat, as it was running outside ??
 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So, you're saying that the app can run inside tomcat, as it was running outside ?


Not quite. You'll need to rewrite it to be part of the web app. For example, whatever the standalone app does in the main method now needs to happen in a ServletContextListener's contextInitialized method, and the contextDestroyed method needs to shut down all threads, sockets and other resources that the code allocates. But the networking stuff itself should work pretty much as is. You'll just need to arrange for all the data it collects to be kept in some data structure that the rest of the web app -which is supposed to display it- can get at.
 
xbhanu Choudhary
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh okay, then i need to study about the ServletContextListener and this contextInitialized method. I just finished a struts tutorial, so thought never have to venture into servlet writing.

Hmmm...so when the app will run in this contextInitialized method, it would run in tomcat's address space so that other servlets can easily access the data from the map. Is it ??

Thanks a lot bud
 
Ulf Dittmer
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

so thought never have to venture into servlet writing.


Servlet context listener are not directly related to servlets: servlets react to HTTP request, whereas servlet context listeners are used for setting up and shutting down web apps. A better name for them would be "web app context listeners".

so when the app will run in this contextInitialized method, it would run in tomcat's address space so that other servlets can easily access the data from the map.


The app will NOT run in the contextInitialized method (which should terminate quickly). The method is used to set up all the background threads that handle the networking stuff.
 
Bartender
Posts: 20940
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets do not run as processes. Nor do they run as threads. Servlets are code resources, and the container (tomcat) will pull a thread from the thread pool to handle requests, including calling the servlets's request processing method.

All resources in a Tomcat JVM reside in the same address space, but not in the same classpath context.

Within a given webapp, an object constructed in the context listener will be available to any part of the webapp, including servlets, Struts/JSF action handlers or whatever. The object is actually available globally within the appserver, but because of the fact that different classpaths are involved, neither finding it nor using it is as easy as it is intra-app access. And, as a general rule, webapps shouldn't be sharing internal resources anyway, since webapps aren't supposed to be strongly coupled.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The desktop application is running as a server and it is gathering data every 60 seconds.

Putting this functionality inside a web application is not a good idea, in my opinion.

Keeping the desktop application separate from a browser-based GUI is a good idea, in my opinion.

All you need to do is (1) modify desktop application to place data in an accessible storage location, and
(2) create a web application that will enable users to view the stored data, e.g. flat-file, relational table, XML-based file, etc.

Aside, attempting to have the web GUI directly access the data objects of the desktop application is uneccessary and overly complicates things. Sounds like a goofy programming excercise, especially when there are more robust options available....
 
xbhanu Choudhary
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys, I am getting some idea about the servlet context listener but it's still a little blur

How exactly does this servletcontextlistener thing works ???

I'll make my "API to the middleware" class implement this interface and do the stuff of its main method in contextinitialized method. Then when started, it all will run as a single process inside tomcat ??? or need to schedule some threads ???

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!