簡體   English   中英

消息驅動 bean,未根據輪詢間隔獲取新電子郵件

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

我嘗試使用消息驅動 bean 獲取 email。 我的目的是捕獲新的電子郵件。 我用過 Jboss 6.1。 我在 gmail 中啟用了 pop3。 當我啟動服務器時,能夠從收件箱中獲取現有的電子郵件。

一旦服務器啟動並且應用程序正在運行,MailListener 不會通知新的 email。 我也嘗試添加輪詢間隔。

有什么建議么。

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;
    }


}

問題可能與此錯誤有關: https://issues.redhat.com/browse/JBAS-8635提供了一個“已修復”的 mail-ra.jar 文件(不知道是否可以信任)。 在他們 state 的評論中,他們只在現有的 mail-ra.jar 中添加了“setReleased”。 有人對其進行了測試,他們確認它有效。

在票的最后評論中還有一個手動實現示例。 以這種方式實現它會給你更多的控制權。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM