aspose file tools*
The moose likes Java in General and the fly likes Storing Object Temporarily Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Storing Object Temporarily" Watch "Storing Object Temporarily" New topic
Author

Storing Object Temporarily

Koms Alam
Greenhorn

Joined: Jul 29, 2008
Posts: 10
We have a requirement like this...

1. We are getting an ArrayList from external system("Client") which contains two CustomMailRequest(CustomMailRequest1 and CustomMailRequest2).
2. Need to send two separate mails( assume Mail1 and Mail2) for these two requests.
3. Mail2 is delivered using third party system.
4. Mail1 is delivered using java mail API. But before sending Mail1 we do some processing inluding asynchronous call to third party system "ES" that takes 5 mins to complete. Mail1 can be sent 5 minutes after this "ES" call is made.
5. Mail2 has dependency on Mail1, ie Mail2 can be sent only after Mail1 is sent.
6. We don't want to block "Client" which sends the ArrayList, for 5 mins. So once we get request from Client, we've thouht to do the following
a. serialize and store the ArrayList, alongwith currentTimeStamp and Status="Unprocessed" temporarily in DB
b. make the asynchronous call to "ES"
c. We'll have WorkManagerAPI that'll run continuosly to get record from DB based on TimeStamp and Status and will make calls to send both the mails Mail1 and Mail2, and then update status="Processed" or delete the record.
***********************************************************************
Now, what all other approaches can be considerd for this apart from storing the object in DB and the having WorkManagerAPI to process them later? We are not supposed to create thread(and make it wait for 5 mins).
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi there and welcome to Javaranch!

It certainly is an interesting scenario!

A couple of questions:
----------------------

1.) You say you don't want to 'block' the Client. I'm not understanding how the client is blocked, is it waiting for you to respond to it (e.g. You telling it that you've successfully sent Mail1 and Mail2?

2.) Is there a particular reason you are not allowed to use threads?

3.) Do you need to store the whole ArrayList? You might be able to get away with just storing CustomMailRequest2.

4.) Are there transactions involved?

5.) I would probably think about having the logic of sending Mail1 'triggering' the retrieval of CustomMailRequest2 and then sending that off as Mail2.

Hope that helps the discussion!


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Koms Alam
Greenhorn

Joined: Jul 29, 2008
Posts: 10
Thanks Martijn for your valuable time and patience to go through it,

Here are the clarifications...

1.)Client doesn't want any reply...however if we don't store the request and start processing to send Mail1 first and then Mail2 immediately afer Client calls our API, these process would take atleast 5 mins. Until Mail2 is delivered Client has to wait in this case.
Instead we store the requets, make asynch call to "ES" and immediately return. This way client need not wait for 5 mins.

2.) The Work Manager service provides a high-level programming model that enables applications to logically execute multiple work items concurrently under the control of the container. In essence, the work manager provides a container-managed alternative to using the java.lang.Thread, which is inappropriate for use within applications hosted in managed environments.
(The Timer and Work Manager for Application Servers specification provides a concurrent programming API for use within managed environments on the Java TM platform, such as Servlets and EJBs)

3.) If we store only CustomMailRequest2, then we need to send Mail1 immediately. But for Mail1, we call "ES" which takes 5 mins to complete.
So, till that time Client has to wait.

4.) No ransactions invloved, but Mail2 should be sent only after Mail1 is sent. We don't bother about Mail2 delivery failure.

5.)WorkManagerAPI will be responsible for what you are saying, ie sending Mail1, and then triggering Mail2.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi there,

I'm still a little confused over 1.). If the client is not waiting for a response then why is it waiting at all?

It might be an idea to decoupling the client call from the actual work of sending the 2 mail messages. For example having hte client posting to a Topic and having your mail handling code subscribing to that topic and performing the actual work (the JMS topic being the point of decoupling), there are lots of other 'patterns' to achieve this.

Hope that made sense!
Koms Alam
Greenhorn

Joined: Jul 29, 2008
Posts: 10
Hi Martijn, thank you again...

Regarding the Client's wait, Client is making a synchronous call. If we start processing both the mails immediately(which means it would take at least 5mins) isn't the client blocked for 5mins ?

Regarding your suggestion of using JMS, client will not be blocked this way.
But how do we process the request?
A listener will get both the Requests(MailRequest1 and 2) from MQ. Now we need to send Mail1 first that includes an asynch call to "ES". After we call "ES" we have to wait for 5 mins to send Mail1. Now how do we wait for 5mins if we are not allowed to use Thread ?
Once Mail1 sent, we can send Mail2.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Originally posted by Koms Alam:
Hi Martijn, thank you again...

Regarding the Client's wait, Client is making a synchronous call. If we start processing both the mails immediately(which means it would take at least 5mins) isn't the client blocked for 5mins?


Exactly! . That's why you need to look a decoupling the client request from the actual work, e.g. Fire and Forget. A technique for doing this is using a decoupling point (such as a JMS Topic, but there are several other options).

Originally posted by Koms Alam:

Regarding your suggestion of using JMS, client will not be blocked this way.
But how do we process the request?
A listener will get both the Requests(MailRequest1 and 2) from MQ. Now we need to send Mail1 first that includes an asynch call to "ES". After we call "ES" we have to wait for 5 mins to send Mail1. Now how do we wait for 5mins if we are not allowed to use Thread ?
Once Mail1 sent, we can send Mail2.


Oh I see, the second problem you have is that the ES call is asynchronous so you really need another thread to 'wait' until you've been notified somehow.

At the risk of making this JMS centric, can you have the ES process place a message on another topic when it has finished its work? You could then have a 2nd listener which instantly 'knows' when ES has finished (because a messsage arrives on its topic) and therefore then goes and gets MailRequest2 from the first topic.

Hope that made sense!
Koms Alam
Greenhorn

Joined: Jul 29, 2008
Posts: 10
Hi Martijn,
We don't have much details(Internal Working) about "ES", What we are told is how to call(invoke API) it. And was told like, after "ES" call we should wait for 5 mins, because internally ES takes maximum 5 mins to complete the process. Had it acknowledged process completion, somehow(like the one mentioned, publishing to some topic)it would have been very helpful to solve the problem. But "ES" is a legacy system. Those people don't want to modify it.
Anyway, your suggestion was very good. Thank you very for your time and reply.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hmm, that is tricky if you get no reply from ES! The only other thing I suggest is that you incorporate either a Quartz Timer or EJB Timer to trigger an event after 5 minutes, this could potentially give you that decoupling without introducing a new (Java created) thread.

Good luck with it all and thanks for posting a great problem!
Koms Alam
Greenhorn

Joined: Jul 29, 2008
Posts: 10
Hi Martijn,

Thanks for your recommendation to use Quartz Timer/EJB Timer.
I want one more help from you.
Which one would be better to use, Quartz Timer or EJB Timer?
(I'm using Rational Application Developer(RAD) 6.0 for development purpose and highest EJB version is 2.1 here.)
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

I personally find the Quartz timers a lot more flexible, the EJB 2.1 and even 3.0 Timers leave lots to be desired (In my opinion).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing Object Temporarily