[ 
https://issues.apache.org/jira/browse/JAMES-1317?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13120028#comment-13120028
 ] 

Michael Herrmann commented on JAMES-1317:
-----------------------------------------

Hi again, 

I did some more testing. Your fix works but unfortunately James now seems to 
hang at a different position. I hope it's due to a wrong API usage on my part. 

In addition to the above rewriting of senders/recipients, I now also store all 
emails I process in a particular user's inbox. The code is more or less taken 
from org.apache.james.transport.mailets.SieveMailet:

    private void storeMailInUserInbox(Mail mail, String user) throws 
MailboxException, MessagingException {
        storeMailInUserMailbox(mail, user, "INBOX");
    }

    /**
     * @throws BadCredentialsException  if unable to authenticate to mailbox
     * @throws MailboxException         if mailbox cannot be accessed or 
logging out from the mailbox failed
     * @throws MailboxNotFoundException if mailbox can neither be found nor 
created
     * @throws MessagingException       if the mail cannot be read
     */
    private void storeMailInUserMailbox(Mail mail, String user, String 
mailboxName) throws MailboxException, MessagingException {
        MailboxSession session = mailboxManager.createSystemSession(user, new 
MailetContextLog(getMailetContext()));
        mailboxManager.startProcessingRequest(session);
        try {
            final MessageManager mailbox = getUserMailbox(session, user, 
mailboxName);
            if (mailbox == null)
                throw new MailboxNotFoundException("Mailbox " + mailboxName + " 
for user " + user + " was not found on this server. ");
            mailbox.appendMessage(new 
MimeMessageInputStream(mail.getMessage()), new Date(), session, true, null);
        } finally {
            session.close();
            MailboxException thrownException = null;
            try {
                mailboxManager.logout(session, true);
            } catch (MailboxException e) {
                thrownException = e;
            } finally {
                mailboxManager.endProcessingRequest(session);
            }
            if (thrownException != null) throw thrownException;
        }
    }

    private MessageManager getUserMailbox(MailboxSession session, String user, 
String mailboxName) throws MailboxException {
        MailboxPath mbxPath = new MailboxPath(MailboxConstants.USER_NAMESPACE, 
user, mailboxName);
        for (MailboxPath existingMbxPath : mailboxManager.list(session))
            if (mailboxName.equals(existingMbxPath.getName()))
                mbxPath = existingMbxPath;
        if (!mailboxManager.mailboxExists(mbxPath, session))
            mailboxManager.createMailbox(mbxPath, session);
        return mailboxManager.getMailbox(mbxPath, session);
    }

It is called from my service() method as follows:

    public void service(Mail mail) throws MessagingException {
        MailAddress from = new MailAddress((InternetAddress) 
mail.getMessage().getFrom()[0]);
        MailAddress to = new MailAddress((InternetAddress) 
mail.getMessage().getRecipients(Message.RecipientType.TO)[0]);
        if (to.getDomain().equals(mailserverConfiguration.getDefaultDomain())) {
            Conversation conversation = 
conversationService.getConversation(mail);

            InternetAddress[] recipients = 
conversation.getAliasB().getRecipients();
            logger.info("Rewriting recipients of mail " + 
mail.getMessage().getSubject() + " to " + Arrays.asList(recipients));
            mail.getMessage().setRecipients(Message.RecipientType.TO, 
recipients);
            mail.getMessage().saveChanges();

            try {
                storeMailInUserInbox(mail, "_admin@localhost");
            } catch (MailboxException e) {
                logger.error("Unable to log mail " + mail, e);
            }

            String newFrom = conversation.getAliasA() + "@" + 
mailserverConfiguration.getDefaultDomain();
            logger.info("Rewriting sender of mail " + 
mail.getMessage().getSubject() + " from " + from + " to " + newFrom + ".");
            mail.getMessage().setFrom(new InternetAddress(newFrom));
            mail.getMessage().saveChanges();

            getMailetContext().sendMail(mail.getMessage());
            mail.setState(Mail.GHOST);
        } else if 
(!from.getDomain().equals(mailserverConfiguration.getDefaultDomain())) {
            String newFrom = 
conversationService.getConversation(mail).getAliasA().toString() + "@" + 
mailserverConfiguration.getDefaultDomain();
            logger.info("Rewriting sender of mail " + 
mail.getMessage().getSubject() + " from " + from + " to " + newFrom + ".");
            mail.getMessage().setFrom(new InternetAddress(newFrom));
            mail.getMessage().saveChanges();
        }
    }

With too many concurrent connections, this now hangs in storeMailInUserMailbox 
resp. getUserMailbox. I'm attaching a threaddump ("Dump2.txt") as well as the 
server logs (logs.zip). Am I using the API correctly? Is there a standard  way 
of storing a Mail in a particular user's inbox, even if the mail is not 
addressed to him, and without stopping further processing of the mail?

Best regards and sorry for taking up even more of your time, 
Michael
                
> Mail spooled but not processed
> ------------------------------
>
>                 Key: JAMES-1317
>                 URL: https://issues.apache.org/jira/browse/JAMES-1317
>             Project: JAMES Server
>          Issue Type: Bug
>          Components: SpoolManager & Processors
>    Affects Versions: 3.0-M1, 3.0-M2, 3.0-beta3
>            Reporter: Raju Buchi
>            Priority: Critical
>             Fix For: 3.0-beta4
>
>         Attachments: Dump.txt, 
> james-server-mailetcontainer-camel-3.0-beta4-SNAPSHOT.jar, 
> james-server-mailetcontainer-camel-3.0-beta4-SNAPSHOT.jar, jstack.zip, 
> logs.zip
>
>
> Mails sent to James server are spooled but they are not processed and sent 
> out.
> The issue is similar to the one faced by Zach.
> The server seems to be working fine for a few hours or few days, but some 
> time all the mails are spooled but they will not be processed and sent out. 
> Restarted the server several times and some time a few of the mails will be 
> processed and all the others are lost.
> As Norman suggested I have took a jstack trace. 
> Please let me how we can resolve this issue
> Thanks
> - Raju

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to