将数据从无状态会话bean移动到文件

时间:2011-07-15 20:51:18

标签: java-ee jboss cgi ejb xml-rpc

我有一个会话bean,它从数据库中检索数据并对其进行格式化。在不久的将来,它需要调用一个实用程序(包装XML-RPC)来转发数据,但是现在,我需要执行一个命令行脚本,它将文件作为输入(即'command -f filename') 。我真的不喜欢从会话bean写一个文件的想法(JBoss会让我这样做吗?),而且我一直在打开一个CGI脚本的URL,但这似乎有点过分了。那么,将这些数据放入文件的最简洁,最简单的方法是什么,以便我可以调用命令?

其他信息:

  • 我们的服务器是JBoss,它不是群集的。
  • 数据可能相当大,可能包含10,000个XML编码数据(如果绝对必要,可以将其分成更小的块)。

TIA, 丙基三甲氧基硅烷

1 个答案:

答案 0 :(得分:0)

这是一种将内容放入文件的相对简单的方法。

在会话bean中,实现类似于:

的业务方法
public Object getContent(String fileName, <Other Args>) {
  // Get content
  // write to a byte array
  byte[] content = ......;
  return new ByteArrayFile(content, fileName);
}

方法实现应该获取数据(来自DB)并将其序列化为字节数组,然后将其加载(以及可选的文件名)到 ByteArrayFile 的实例中并返回。 / p>

ByteArrayFile 是一个可序列化的对象,它实现了一个 readResolved 方法,该方法将字节数组转换为本地文件(使用提供的名称或临时文件)并返回调用者作为 java.io.File

这是 ByteArrayFile 的粗略实现:

public class ByteArrayFile implements Serializable {
    protected final byte[] content;
    protected final String fileName;

    /**
     * Creates a new ByteArrayFile
     * @param content The file content
     */
    public ByteArrayFile(byte[] content) {
        this(content, null);
    }

    /**
     * Creates a new ByteArrayFile
     * @param content The file content
     * @param fileName The file name to deserialize as
     */
    public ByteArrayFile(byte[] content, String fileName) {
        super();
        this.content = content;
        this.fileName = fileName;
    }

    /**
     * Returns this object as a resolved file when the object is deserialized.
     * @return
     * @throws ObjectStreamException
     */
    protected Object readResolve() throws ObjectStreamException {
        FileOutputStream fos = null;
        try {
            File f = fileName==null ? File.createTempFile(getClass().getSimpleName(), ".file") : new File(fileName);
            if(!f.canWrite()) {
                throw new Exception("Unable to write to designated file [" + fileName + "]", new Throwable());              
            }
            fos = new FileOutputStream(f);
            fos.write(content);
            fos.close();
            fos = null;
            return f;
        } catch (Exception e) {
            throw new RuntimeException("Failed to readResolve", e);
        } finally {
            try { if(fos!=null) fos.close(); } catch (Exception e) {}
        }
    }
}

这是一个简化的(即非远程EJB调用)示例,用于显示创建 ByteArrayFile ,对其进行序列化,然后将其作为文件读回:

public static void main(String[] args) {
    ByteArrayFile baf = new ByteArrayFile("Hello World".getBytes());
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(baf);
        oos.flush();
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        File file = (File)ois.readObject();
        System.out.println("File:" + file.getAbsolutePath() + "  Size:" + file.length());
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

输出结果为:

  

文件:/tmp/ByteArrayFile2790187442947455193.file文件大小:11

事实上,你的会话bean绝对可以直接写一个文件。严格的EJB限制不是由JBoss强制执行的,而是为了提供您可能不关心的可移植性保证而存在。但是,上述方法的好处是远程客户端可以远程调用该调用,但在本地获取该文件。

相关问题