如何将 javax.mail.Message 的失败自定义对象放入 jms 队列(使用 Activemq)

How to put failed custom object of javax.mail.Message into jms queue (using Activemq)

我正在创建使用我的 outlook 帐户发送邮件的实用程序,为此我正在创建 javax.mail.Message 的对象并发送它,如果消息发送由于 SendingFailedException 而失败,我想将这些消息添加到jms 队列,另一端的侦听器将 运行 每 10 分钟从队列中消费这些消息并尝试重新发送这些消息。

我已经浏览了一些与之相关的 Whosebug 主题,他们指示将消息更改为 xml 或 JSON,我只是想知道如何处理它,如果那样的话将是实现这一点的方式。

提前致谢

使用 MimeMessage.writeTo method you can turn the message into a byte stream. Collect it in a ByteArrayOutputStream and then include the bytes in the JMS message. At the other end, you can reconstitute the message using the MimeMessage constructor that takes an InputStream.

例如:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
msg.writeTo(bos);
byte[] data = bos.toByteArray();
// put the data in a JMS message

// in the receiver, extract the byte array from the message
byte[] data = ...
MimeMessage msg = new MimeMessage(session, data);

抱歉,我无法在 JMS 部分帮助您。