Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Servlet to Servlet Communication

Reply
Thread Tools

Servlet to Servlet Communication

 
 
MattC
Guest
Posts: n/a
 
      10-12-2005
My application uses 2 Servlets - ServletMain and ServletPoll.
ServletMain is the main Servlet for my MVC application. ServletPoll is
responsible for polling for data files. The idea is that every 10
minutes ServletPoll will "wake up" and check to see if there are any
new data input files to process. If there is it will process these
files and then go back to sleep for 10 minutes and start the cycle all
over again.

The problem I have is that I do not no how to start the method in
ServletPoll that will loop forever checking for data input files.

I have set up the web.xml file so that ServletMain and ServletPoll get
loaded when the web app is deployed. Now the question is how can I
invoke the method to start the polling. This needs to begin as soon as
the web app is deployed.

Is it possible to invoke ServletPoll's polling method from the init()
method of ServletMain?

 
Reply With Quote
 
 
 
 
Alun Harford
Guest
Posts: n/a
 
      10-12-2005
"MattC" <> wrote in message
news: oups.com...
> My application uses 2 Servlets - ServletMain and ServletPoll.
> ServletMain is the main Servlet for my MVC application. ServletPoll is
> responsible for polling for data files. The idea is that every 10
> minutes ServletPoll will "wake up" and check to see if there are any
> new data input files to process. If there is it will process these
> files and then go back to sleep for 10 minutes and start the cycle all
> over again.
>
> The problem I have is that I do not no how to start the method in
> ServletPoll that will loop forever checking for data input files.
>
> I have set up the web.xml file so that ServletMain and ServletPoll get
> loaded when the web app is deployed. Now the question is how can I
> invoke the method to start the polling. This needs to begin as soon as
> the web app is deployed.
>
> Is it possible to invoke ServletPoll's polling method from the init()
> method of ServletMain?
>


I've not tried it myself, but I'd think calling the method in the
static {
....
}
block would do it.

--
Include the word 'lemongrass' in any email you send to me, or you'll hit my
spam filter. If you're reading archives, I may have changed this word -
check http://www.alunharford.co.uk/


 
Reply With Quote
 
 
 
 
Ross Bamford
Guest
Posts: n/a
 
      10-12-2005
On Wed, 12 Oct 2005 17:56:25 +0100, Alun Harford
<> wrote:

> "MattC" <> wrote in message
> news: oups.com...
>> My application uses 2 Servlets - ServletMain and ServletPoll.
>> ServletMain is the main Servlet for my MVC application. ServletPoll is
>> responsible for polling for data files. The idea is that every 10
>> minutes ServletPoll will "wake up" and check to see if there are any
>> new data input files to process. If there is it will process these
>> files and then go back to sleep for 10 minutes and start the cycle all
>> over again.
>>
>> The problem I have is that I do not no how to start the method in
>> ServletPoll that will loop forever checking for data input files.
>>
>> I have set up the web.xml file so that ServletMain and ServletPoll get
>> loaded when the web app is deployed. Now the question is how can I
>> invoke the method to start the polling. This needs to begin as soon as
>> the web app is deployed.
>>
>> Is it possible to invoke ServletPoll's polling method from the init()
>> method of ServletMain?
>>

>
> I've not tried it myself, but I'd think calling the method in the
> static {
> ...
> }
> block would do it.
>


It certainly would. However, if you're in a 2.3+ servlets environment, you
can also implement javax.servlet.ServletContextListener to receive events
at application startup and shutdown.

See
http://java.sun.com/j2ee/1.4/docs/ap...tListener.html

--
Ross Bamford -
 
Reply With Quote
 
Juha Laiho
Guest
Posts: n/a
 
      10-12-2005
"MattC" <> said:
>My application uses 2 Servlets - ServletMain and ServletPoll.
>ServletMain is the main Servlet for my MVC application. ServletPoll is
>responsible for polling for data files. The idea is that every 10
>minutes ServletPoll will "wake up" and check to see if there are any
>new data input files to process.


No, this doesn't sound like a servlet. A servlet wakes up on external
trigger, handles the event and goes back to sleep. It doesn't, by
itself, wake up periodically.

I'm seconding the comment from Ross that a ServletContextListener
would be a better place to hook up this kind of functionality.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
 
Reply With Quote
 
MattC
Guest
Posts: n/a
 
      10-12-2005
You recommend using a static block:
"static {

}

I am unclear exactly what you mean. Where should the static block be
placed, in ServletMain or ServletPoll? If in ServletPoll can I assume
that by the time the static block is executed the class is fully
constructed and the polling method available?

 
Reply With Quote
 
MattC
Guest
Posts: n/a
 
      10-12-2005
ServletPoll is a servlet dedicated to periodically retrieving data
files. As such my idea is that it will be put into a continuous loop.
Every 10 minutes it will check for data files and process them. It will
then wait - System.wait(); - and start the cycle all over again.

Is there a simpler way to do this?

 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      10-12-2005
On Wed, 12 Oct 2005 21:37:55 +0100, MattC <>
wrote:

> ServletPoll is a servlet dedicated to periodically retrieving data
> files. As such my idea is that it will be put into a continuous loop.
> Every 10 minutes it will check for data files and process them. It will
> then wait - System.wait(); - and start the cycle all over again.
>
> Is there a simpler way to do this?
>


Start with something along the lines of:

<pseudocode>
public class LifecycleListener implements
javax.servlet.ServletContextListener {

private volatile Thread manager;

private static class Manager implements Runnable {
public void run {
Thead me = Thread.currentThread();
while (manager == me) {

// Do your stuff here

try {
Thread.sleep(10000)
} catch (InterruptedException e) {
// could be we're to stop, or just a 'spurious wakeup'
}
}
}
}

public void contextInitialized(...) {
manager = new Thread(new Manager());
manager.start()
}

public void contextDestroyed(...) {
Thread t = manager;
manager = null;
t.interrupt();
}
}
</pseudocode>

And then look into the Servlets docs, since there are some things you need
to be aware of with this kind of processing.

--
Ross Bamford -
 
Reply With Quote
 
HalcyonWild
Guest
Posts: n/a
 
      10-13-2005

Ross Bamford wrote:
> Start with something along the lines of:
>
> <pseudocode>
> public class LifecycleListener implements
> javax.servlet.ServletContextListener {
>
> private volatile Thread manager;
>
> private static class Manager implements Runnable {
> public void run {
> Thead me = Thread.currentThread();
> while (manager == me) {
>
> // Do your stuff here
>
> try {
> Thread.sleep(10000)
> } catch (InterruptedException e) {
> // could be we're to stop, or just a 'spurious wakeup'
> }
> }
> }
> }
>
> public void contextInitialized(...) {
> manager = new Thread(new Manager());
> manager.start()
> }
>
> public void contextDestroyed(...) {
> Thread t = manager;
> manager = null;
> t.interrupt();
> }
> }
> </pseudocode>
>


Hi ,

ServletContextListener is one method. I was also thinking about this
problem , and ServletContextListener did not occur to me.

I thought, in the init() of ServletPoll, we can call another plain
Runnable java class. I do not know if there is something wrong in doing
this, but I am not sure. Is there anything wrong with doing this.
The only thing I can think of is that , the server thread running might
be blocked by ServletPoll, while ServletMain, waits to get its control,
to process subsequent requests.
Or might be I am wrong. There are two separate server threads for
ServletMain and ServletPoll. Please advise. Thanks.

 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      10-13-2005
On Thu, 13 Oct 2005 11:40:57 +0100, HalcyonWild <>
wrote:

>
> Ross Bamford wrote:
>> Start with something along the lines of:
>>
>> <pseudocode>
>> public class LifecycleListener implements
>> javax.servlet.ServletContextListener {
>>
>> private volatile Thread manager;
>>
>> private static class Manager implements Runnable {
>> public void run {
>> Thead me = Thread.currentThread();
>> while (manager == me) {
>>
>> // Do your stuff here
>>
>> try {
>> Thread.sleep(10000)
>> } catch (InterruptedException e) {
>> // could be we're to stop, or just a 'spurious wakeup'
>> }
>> }
>> }
>> }
>>
>> public void contextInitialized(...) {
>> manager = new Thread(new Manager());
>> manager.start()
>> }
>>
>> public void contextDestroyed(...) {
>> Thread t = manager;
>> manager = null;
>> t.interrupt();
>> }
>> }
>> </pseudocode>
>>

>
> Hi ,
>
> ServletContextListener is one method. I was also thinking about this
> problem , and ServletContextListener did not occur to me.
>
> I thought, in the init() of ServletPoll, we can call another plain
> Runnable java class. I do not know if there is something wrong in doing
> this, but I am not sure. Is there anything wrong with doing this.
> The only thing I can think of is that , the server thread running might
> be blocked by ServletPoll, while ServletMain, waits to get its control,
> to process subsequent requests.
> Or might be I am wrong. There are two separate server threads for
> ServletMain and ServletPoll. Please advise. Thanks.
>


I wouldn't suggest going down that route. If I understand you correctly
you're suggesting something like the following:

<pseudocode>
public class ServletPoll extends javax.servlet.http.HttpServlet {

public void init() {

Runnable lifecycleMgr = new LifecycleManager();
lifecycleMgr.run();

}

public void doGet(HttpServletRequest req, HttpServletResponse resp) {

// what on earth goes here?

}
}
</pseudocode>

IIRC this is a very bad idea, since theres probably no guarantee that the
init() calls will be made on separate threads (you should check the
Servlet spec to be sure because I haven't), and just calling the run()
method on a runnable does NOT start a new thread. So not only would your
init() method fail to return (bad in itself) but you may well prevent
other servlets being initialized too. The server would probably not hook
it up to service requests, which is what I think you mean when you say
ServletPoll might block the server thread.

The thread model with Servlets is actually slightly more complex than you
imply, and usually involves a thread pool, with the next free thread
handling requests as they come in. I think the spec is fairly non-specific
about the specifics of the threading model, and instead chooses a very
open-ended approach (if it doubt, assume it's not safe), to allow servers
to manage the threading how they choose according to the performance and
resource requirements.

Of course, the real question is, why is ServletPoll even a Servlet? What
is it serving, i.e. what would you be doing with the doGet, doPost, or
whatever methods, in that class? A servlet should aim to process
individual requests, in any order, generating responses as appropriate, as
quickly and with as little shared state as possible (read: none). Think
the MVC controller. Another, entirely separate part is application,
request and session lifecycle and initialization, and for this the spec
provides separate interfaces to hook into these things, which in this
particular case means ServletContextListener.

If you are determined, you could probably get away with _starting a new
thread_ from the ServletPoll.init() method, allowing the method to return.
I'd guess you'd be on shaky ground, however, with regard to running on
different app servers and platforms, and you would of course need to
consider graceful shutdown of your new thread - not just for server
shutdown but also because it's possible to dynamically deploy and undeploy
apps in most containers.

--
Ross Bamford -
 
Reply With Quote
 
Alun Harford
Guest
Posts: n/a
 
      10-14-2005
"MattC" <> wrote in message
news: oups.com...
> You recommend using a static block:
> "static {
>
> }
>
> I am unclear exactly what you mean. Where should the static block be
> placed, in ServletMain or ServletPoll? If in ServletPoll can I assume
> that by the time the static block is executed the class is fully
> constructed and the polling method available?
>


ServletPoll. And yes, the block is executed immediately AFTER the class gets
loaded (as per java standards).
(Although what you say you want doesn't seem to be what you actually want,
based on what you've said - Why is ServletPoll a servlet?)

Alun Harford

--
Include the word 'lemongrass' in any email you send to me, or you'll hit my
spam filter. If you're reading archives, I may have changed this word -
check http://www.alunharford.co.uk/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Applet Servlet communication via HTTPS Ng Wee Peng Java 1 04-01-2004 06:21 PM
Applet <==> Servlet Communication Mustafa Aydin Java 7 11-20-2003 03:26 PM
Applet to servlet communication Rui Pacheco Java 2 10-23-2003 12:06 AM
Swing-servlet communication? NDerTfr Java 0 09-08-2003 05:00 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57