views:

319

answers:

2

I'm using java mail api 1.4.1 to obtain new emails. Two classes are being used to obtain emails and then parsing it. "GetMail" class communicates with mail server(Gmail,yahoo etc) and obtains the message object. Then the message object is passed to yet another class "MailFormatter" class, which then parses the message object, obtains the email headers (From,To,Subject etc) and then it parses the Multipart content to obtain the main body and attachments.Since both "Mail getting" and "Mail formatting" process are very resource intensive, these classes are going to be implemented as separate web applications.This application is going to monitor new emails for numerous email ids.If these ("GetMail" and "MailFormatter") are implemented as separate web applications, how can I pass the message object from "GetMail" app to "MailFormatter" app ? Is there a way through which I can persist the obtained message object in a certain location (a location which is common to both "GetMail" and "MailFormatter" applications), so that "GetMail" can persist the message object in that location, and then "MailFormatter" app can read "Message" objects from that location and carry out the parsing process. Message objects cannot be serialized. If they cannot be serialized how can I persist the state of java mail message object? please do help me to resolve this issue.

A: 

Well MimeMessage can be serialized with writeTo(OutputStream) and deserialized with the constructor MimeMessage(Session,InputStream).

So a message can be sent from one webapp to the other.

Maurice Perry
thanks for answering. So I can use FileOutputStream and persist object as a file and then read it via MimeMessage constructor. Is FileOutputStream the best one to persist message? What if the persisted message is not MimeMessage? How can I read non-mime messages via input stream? Also MimeMessage constructor is demanding a session object for reading the message input stream. How can I create this session object in the other web application?
jezhilvalan
A: 

I realise I'm not answering your question (!) but I wonder if you're solving the right problem. Is the parsing so time consuming that it requires splitting into a different process. Have you tried gathering the messages via one thread, and parsing them asynchronously via another ? I suspect this would be simpler than performing some form of IPC.

If you do want to serialise objects that aren't Serializable, take a look at XStream, which will serialise most things to/from XML.

Brian Agnew