开发者

Deserialize MimeMessages JavaMail

开发者 https://www.devze.com 2023-04-03 20:28 出处:网络
Good afternoon in my timezone. I am working with JavaMail api with exJello Provider. I am using SearchTerm class to filter the retrieved messages but it takes in average more than 1 minute to the sea

Good afternoon in my timezone.

I am working with JavaMail api with exJello Provider. I am using SearchTerm class to filter the retrieved messages but it takes in average more than 1 minute to the search method returns results.So i decided to serialize a set of messages this way i did not have to wait so long.so i have one theoretical issue and one specif issue. 1) Only the classes that implements the Serializable interface could be serialize so the way i use to "serialize" this messages is not a "really" serialization, right ? Snippet of my code : message.writeTo("OutputStream");

2) Now the problem that i am dealing with: Snippet of code:

  messages = inbox.search(new AndTerm(terms));
  ObjectOutputStream stream = new ObjectOutputStream(new  FileOutputStream("serializer.txt"));
   for(Message msg : messages){
      msg.writeTo(stream);
    }

In the end of the process i had serialize more than one message in the file "serializer.txt". My question is how can i deserialize those messages.I already am able to deserialize one messeage , but if the file contains more than one message only the first one get deserialize. Code:

ObjectInputStream file = new ObjectInputStream(new FileInputStream("serializer.txt"));
new MimeMessage(session,file);

This code will deserialize just one mess开发者_Python百科age , but if i make a cicle only the first one will be again deserialize. So any body had face the same issue. PS-> If i try to use the method readObject from any InputStream it will retrieve an exception, the only way is to use the Message constructor.

With the best regards


You can try ObjectInputStream.readObject()

ObjectInputStream messages= new ObjectInputStream(new FileInputStream("serializer.txt"));
while(messages.available()){
   MimeMessage message = (MimeMessage)messages.readObject()
   ....
}


You can use either serilizable or externalizable interface for serialization . Serializable is a marker interface and java will do the serialization for you . If you use externalizable you can write your own serialization method .

If we go according to the definition of serialization then what you are doing can be termed serialization . Coz you are writing your object to a file and are able to restore from it.

In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and "resurrected" later in the same or another computer environment.

For the next part :: do not write all serialized objects in the same file . Create a file dynamically for each serialized object. Use a naming structure for identifying the correct file you want to desirialize and that will solve your problem

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号