简体   繁体   中英

Message Driven bean, is not fetching the new emails based on polling interval

I tried to fetch email using message driven bean. My intention is to capture the new emails. I have used Jboss 6.1. I enabled the pop3s in my gmail. When i start the server able to fetch the existing emails from inbox.

Once the server stated up and application is running, the new email are not notified by MailListener. i tried by adding the polling interval as well.

Any suggestions.

package uk.co.test.inbound;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.inject.Named;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Message.RecipientType;

import org.jboss.ejb3.annotation.ResourceAdapter;
import org.jboss.resource.adapter.mail.inflow.MailListener;


@MessageDriven(
            activationConfig = { 
            @ActivationConfigProperty(propertyName = "mailServer", propertyValue = "pop.gmail.com"),
            @ActivationConfigProperty(propertyName = "mailFolder", propertyValue = "INBOX"),
            @ActivationConfigProperty(propertyName = "storeProtocol", propertyValue = "pop3s"),
            @ActivationConfigProperty(propertyName = "userName", propertyValue = "xxxx@gmail.com"),
            @ActivationConfigProperty(propertyName= "password", propertyValue="xxxxx"),
            @ActivationConfigProperty(propertyName= "pollingInterval", propertyValue="100"),
            @ActivationConfigProperty(propertyName= "port", propertyValue="995"),
            @ActivationConfigProperty(propertyName= "debug", propertyValue="true"),
            @ActivationConfigProperty(propertyName= "starttls", propertyValue="true")
            })
@ResourceAdapter("mail-ra.rar")
@Named("mailListener")
public class EmailReceiver implements MailListener{

    @Override
    public void onMessage(Message msg) {
        System.out.println("One new Message Received" );
        try {

            Address[] fromAddress = msg.getFrom();
            String from = fromAddress[0].toString();
            String subject = msg.getSubject();
            String toList = parseAddresses(msg.getRecipients(RecipientType.TO));
            String ccList = parseAddresses(msg.getRecipients(RecipientType.CC));
            String sentDate = msg.getSentDate().toString();

            String contentType = msg.getContentType();
            String messageContent = "";

            if (contentType.contains("text/plain") || contentType.contains("text/html")) {
                try {
                    Object content = msg.getContent();
                    if (content != null) {
                        messageContent = content.toString();
                    }
                } catch (Exception ex) {
                    messageContent = "[Error downloading content]";
                    ex.printStackTrace();
                }
            } else { 
                try {
                    if (msg.getContent() instanceof Multipart) {
                        Multipart content = (Multipart) msg.getContent();
                        content.getCount();
                        Part part = content.getBodyPart(0);
                        InputStream is = part.getInputStream();
                        if (!(is instanceof BufferedInputStream)) {
                            is = new BufferedInputStream(is);
                        }
                        int c;
                        final StringWriter sw = new StringWriter();
                        while ((c = is.read()) != -1) {
                            sw.write(c);
                        }

                        if (!sw.toString().contains("<div>")) {
                            messageContent = sw.toString();
                        }
                    }
                } catch (IOException e) {
                    messageContent = "[Error downloading content]";
                    e.printStackTrace();
                }
            }


            System.out.println("\t Subject: " + subject);

            System.out.println("\t Message: " + messageContent);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

    /**
     * Returns a list of addresses in String format separated by comma
     *
     * @param address an array of Address objects
     * @return a string represents a list of addresses
     */
    private String parseAddresses(Address[] address) {
        String listAddress = "";

        if (address != null) {
            for (int i = 0; i < address.length; i++) {
                listAddress += address[i].toString() + ", ";
            }
        }
        if (listAddress.length() > 1) {
            listAddress = listAddress.substring(0, listAddress.length() - 2);
        }

        return listAddress;
    }


}

Problem might be linked to this bug: https://issues.redhat.com/browse/JBAS-8635 There is a "fixed" mail-ra.jar file provided (don't know if it can be trusted). In the comments they state they only added a "setReleased" to the existing mail-ra.jar. Someone tested it and they confirmed it worked.

There is also a manual implementation example in the last comment on the ticket. Implementing it that way will give you more controll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM