File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a method from a separte application?

 
Timothy Sam
Ranch Hand
Posts: 751
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! Is this possible? I have two applications. One is a webapp that runs in tomcat and uses tomcat's connection pooling for database access and a standalone application which is run in the command line. I would like to know if it's possible to call my database Class's methods through a separate application? For example, I have a list of Employees in the standalone app and I need to populate each by calling a method in the tomcat application.

ArrayList<Employees> eList = //call from tomcat app that returns an Employee List or ArrayList

Is this possible? Thanks!
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it's possible to call into another application by a number of means (sockets, RMI, web services, ...), can you clarify why the method needs to run inside of the web app? Can't the relevant classes be part of the CLI application as well? Or do you need access to runtime objects of the web app?
 
Timothy Sam
Ranch Hand
Posts: 751
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it's possible to call into another application by a number of means (sockets, RMI, web services, ...), can you clarify why the method needs to run inside of the web app? Can't the relevant classes be part of the CLI application as well? Or do you need access to runtime objects of the web app?



Hi, I think what I need are runtime Connection object of the application. Right now, when I try to run the app I get this error.


E:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\zstats\WEB-INF\cl
asses>java dk/zmag/http/process/DailyUpdate
Starting daily backup...
javax.naming.NoInitialContextException: Need to specify class name in environmen
t or system property, or as an applet parameter, or in an application resource f
ile: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at dk.zmag.http.dao.ConnectionPoolManager.getConnectionFromPool(Connecti
onPoolManager.java:43)
at dk.zmag.http.dao.DAOEssentials.acquireConnection(DAOEssentials.java:1
9)
at dk.zmag.http.dao.mysql.MySQLMagazineDAO.selectAllMagazineFromDB(MySQL
MagazineDAO.java:251)
at dk.zmag.http.process.DailyUpdate.makeUpdate(DailyUpdate.java:45)
at dk.zmag.http.process.DailyUpdate.main(DailyUpdate.java:95)
java.lang.NullPointerException
at dk.zmag.http.dao.mysql.MySQLMagazineDAO.selectAllMagazineFromDB(MySQL
MagazineDAO.java:255)
at dk.zmag.http.process.DailyUpdate.makeUpdate(DailyUpdate.java:45)
at dk.zmag.http.process.DailyUpdate.main(DailyUpdate.java:95)


Connection pooling works ok in my webapp but not in my standalone app. I didn't want to rewrite the database methods that I already created that's why I chose to put the standalone app inside the webapp itself. The standalone app will be called using a cron scheduler to make daily backups of the database.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me see if I understand the situation.

1) You have a webapp in Tomcat, which are doing CRUD-stuff on a database.

2) You (want to) have an application doing other stuff on the database too.

In order to achieve 2), you want to reuse the stuff you did in 1) as much as possible. Sensible.

I didn't want to rewrite the database methods that I already created that's why I chose to put the standalone app inside the webapp itself.


Have you considered moving your database methods outside the webapp instead of moving your standalone app inside the webapp? Make a separate project and .jar-file which you call from either of your apps?
[ December 19, 2006: Message edited by: �dne Brunborg ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

Tomcat provides a naming service that you can access via JNDI from your web application. You probably declare the Tomcat connection pool in a Tomcat XML configuration file (server.xml). In your web application, you're looking up the database connection pool that Tomcat has put in the naming directory for you.

When you run a stand-alone application (outside of Tomcat), you don't have access to Tomcat's naming service directly. So when you try to lookup something via JNDI, it will not be found - JNDI isn't even configured (that's what the error message says).

Ofcourse you can access the database from a stand-alone application, but you can't use Tomcat's connection pool easily; you need to make sure that you connect to Tomcat's naming service to do this. It might be easier to just make a directy JDBC connection to the database from your stand-alone tool. The stand-alone tool is a tool to backup the database, isn't it? It doesn't really need to use a connection pool anyway. (One of the reasons to use a connection pool in a web app is to control the number of connections to the database, regardless of how many users are using the web app - your backup tool just needs 1 connection).
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only know this in passing -- have not using JNDI in Tomcat much, so take this with a grain of salt.

I believe there is a way to turn on remote JNDI service in Tomcat. Something with activating / running some kind of service in Tomcat, that will sit on a fix known port, and route requests to the internal JNDI service.

I thought that I saw examples of this, with either the Apache or JBoss documentations... not much help, but maybe this is a small start.

Henry
 
Timothy Sam
Ranch Hand
Posts: 751
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! I just made a workaround where the standalone app makes the usual way of getting a connection (if connection pooling fails, i.e. it will). Makes my life easier and I still use the database classes from the web app.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic