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

发布于 2024-11-24 04:48:40 字数 364 浏览 2 评论 0原文

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

附加信息:

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

TIA, 伊兰

I have a session bean which retrieves data from the database and formats it. In the near future it will need to call a utility (which wraps XML-RPC) to forward the data, but for now, I need to execute a command line script which takes a file as input (i.e. 'command -f filename'). I don't really like the idea of writing a file from a session bean (and would JBoss let me do this?), and I've been toying with opening a URL to a CGI script, but that seems overkill. So, what is the cleanest, simplest way to get this data into the file, so that I can invoke the command?

Additional info:

  • Our server is JBoss and it is not clustered.
  • The data may be sizable, possibly containing 10,000 pieces of XML encoded data (this can be broken into smaller chunks if absolutely necessary).

TIA,
Ilane

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2024-12-01 04:48:40

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

在会话 bean 中,实现一个如下所示的业务方法:

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

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

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.文件大小:11

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

Here's a relatively simple approach to get your content into a file.

In your session bean, implement a business method that looks something like:

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

The method implementation should acquire the data (from the DB) and serialize it to a byte array which is then loaded (along with an optional file name) into an instance of ByteArrayFile and returned.

The ByteArrayFile is a serializable object which implements a readResolved method which will convert the byte array into a local file (using the provided name or a temp file) and returned to the caller as a java.io.File.

Here's a rough implementation of 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) {}
        }
    }
}

This is a simplified (i.e. not a remote EJB call) example to show creating a ByteArrayFile, serializing it and then reading it back as a File:

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);
    }
}

The output was:

File:/tmp/ByteArrayFile2790187442947455193.file Size:11

In truth, your session bean can absolutely write a file directly. The strict EJB limitations are not enforced by JBoss and exist to provide portability guarantees which you may not care about. However, the benefit of the above approach is that a remote client can invoke the call remotely but acquire the file locally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文