用于发送多个文档的JMS BytesMessage



我有一个批处理作业的需求,应该发送电子邮件。然而,我必须打破这种方式,我需要通过消息传递将电子邮件的详细信息移交给另一个系统,然后该系统将读取所放置的消息并悠闲地发送电子邮件。

我的邮件有附件。我该如何做到这一点?一封给定的电子邮件可能有多个附件。

我读关于字节消息,但我如何使用它的多个附件为一个给定的电子邮件消息?

对此有什么想法吗?

您可以发送一个包含电子邮件详细信息(from, to list, subject, text)的JMS消息,然后发送附件作为JMS字节消息,每个附件消息具有相同的自定义标识符。

发送方

// create JMS Bytes message with mail content
// MailData class should implement java.io.Serializable)
MailData mailData = new MailData();
// emailID could be GUID or anything else that would uniquely identify mail
mailData.setEmailID(emailID);
mailData.setFrom(from);
mailData.setToList(toList);
mailData.setSubject(subject);
mailData.setText(text);
BytesMessage msg = session.createBytesMessage();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(mailData);
out.close();
bos.close();
msg.writeBytes(bos.toByteArray());
producer.send(msg);
// for the sake of simplicity, object attachment contains attachment name, MIME type
// and value as byte array
for(Attachment att : attachmentList) {
    BytesMessage msgAtt = session.createBytesMessage();
    // emailID
    msgAtt.setStringProperty("emailId", emailID);
    msgAtt.setStringProperty("fileName", att.getAttachmentName());
    msgAtt.setStringProperty("mimeType", att.getMimeType());
    // set bytes message payload to attachment content
    msgAtt.writeBytes(att.getValue());
    producer.send(msgAtt);
}

接收端

BytesMessage bytesMessage = (BytesMessage) message;
byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
bytesMessage.readBytes(bytes);
ByteArrayInputStream bis = new ByteArrayInputStream(bytesMessage);
ObjectInputStream in = new ObjectInputStream(bis);
MailData mailData = (MailData) in.readObject();
in.close();
bis.close();
// get data from queue with the same emailID
MessageConsumer consumer = session.createConsumer(queue, "emailID='"
                + mailData.getEmailID() + "'");
connection.start();
Message attMsg = null;
while((attMsg = consumer.receiveNoWait()) != null) {
    String fileName = attMsg.getStringProperty("fileName");
    String mimeType = att.getStringProperty("mimeType");
    BytesMessage bytesMessage = (BytesMessage) attMsg;
    byte[] attachmentValue = new byte[(int) bytesMessage.getBodyLength()];
    bytesMessage.readBytes(attachmentValue);
}

相关内容

  • 没有找到相关文章

最新更新