端标记符——在实际的应用中,这个客户端标记符可被修改为全局的唯一标记符。CurrentReport是非线程安全的;我们假设在客户端应用中只有一个单线程执行服务器事务。
beginTransaction() 创建一个新的ClientReport,设置它的客户端标记符并且记录事务起始时间。commitTransaction() 计算事务的毫秒数并且保存最后一个调用的副本,以方便以后的到服务器的上传。
Serializer 是Payload 软件包中的第三个类。客户端和服务器都要使用者各类。客户端使用attachPendingReportToMessage()连载待解决的当作XML的ClientReport ,并将它当作文本附件添加到SOAP信息中。服务器使用queueFirstAttachmentText()剥离信息附件并将它列队等待处理次数报告的、信息驱动的EJB组件的使用:
package Payload;
import java.io.*;
import java.util.Iterator;
import java.beans.*;
import javax.xml.rpc.handler.soap.*;
import javax.xml.soap.*;
import javax.jms.*;
import javax.naming.*;
/**
*
* @author Brian Connolly Brian@ideajungle.com
*/
public class Serializer {
// Common queue connections used by queueFirstAttachment
private static Context jndiContext = null;
private static QueueConnectionFactory queueConnectionFactory = null;
private static QueueConnection queueConnection = null;
private static QueueSession queueSession = null;
private static Queue queue = null;
private static QueueSender queueSender = null;
private static boolean connectionEstablished = false;
/** Creates a new instance of Serializer */
public Serializer() {
}
private static String ClientReportXML(ClientReport r) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLEncoder sstream = new XMLEncoder(baos);
sstream.writeObject(r);
sstream.flush();
sstream.close();
return baos.toString();
}
public static ClientReport ClientReportXML(String crXML) {
ByteArrayInputStream bais = new ByteArrayInputStream(crXML.getBytes());
XMLDecoder sstream = new XMLDecoder(bais);
return (ClientReport)sstream.readObject();
}
public static void attachPendingReportToMessage(SOAPMessageContext smc) {
try{
ClientReport cr = CurrentReport.getReport();
if (cr != null) {
SOAPMessage mc = smc.getMessage();
AttachmentPart ap = mc.createAttachmentPart(Serializer.ClientReportXML(cr),new String("text/plain"));
mc.addAttachmentPart(ap);
}
}
catch(Exception e){
// Make sure that application processing proceeds undisturbed
}
}
public static void queueFirstAttachmentText(SOAPMessageContext smc) {
String sattachment;
try{
SOAPMessage mc = smc.getMessage();
Iterator attachments = mc.getAttachments();
if(attachments.hasNext()){
AttachmentPart attachment = (AttachmentPart)attachments.next();
sattachment = attachment.getContent().toString();
attachments.remove();
queueReportXML(sattachment);
}
}
catch (SOAPException e){
System.out.println("Queue Attachment exception:" + e.toString());
}
}
/*
|