Java EE, EJBs File handling

2019-03-21 17:23发布

I'm developing a web application in which users are allowed to upload pictures, the system will then generate thumbs for them.

My problem relies on the fact that EJBs can be distributed on several servers and thus are not allowed to handle files directly. I could store the images in the databases but I was hoping to store them as files in one of the servers. How can I do this? Is there any way to centralize the storage of files? Or any approach to deal with files in Java EE with EJBs?


Currently, I'm storing my files in a database. So I have centralized access and I don't need a dedicated file server. I'm doing this because I don't know how to integrate ftp servers and EJBs. Is this however a good alternative?

What I want is: Using Stateless EJBs, store the uploaded images as files and the path to them in the database. So I can display them using

<h:graphicImage ... />

6条回答
祖国的老花朵
2楼-- · 2019-03-21 17:37

You actually have four aspects here,

  1. Receiving and sending files
  2. Creating thumbnails
  3. Storing the files somewhere every node can access
  4. Storing the original + thumbnail entities in a common database

Since you already have Java EE server, you probably also already have a (HTTP) servlet server, in which there is numerious ways of doing load balancing and caching, not to mention the obious potential for web-based interaction. If anything, support FTP transfer with a directory watcher as a bonus.

You should not create the thumbnails using stateless session beans, this means your servers will be crap at peak time - the server will give priority to buisness logic over making new connections. Rather, first receieve and store the file + original entity in the database, and then use a service bean to queue up thumbnail creation (maybe with n worker threads or message queues if you want). You can also use native tools in some cases, we do in linux.

You should use a shared file system, SAN, which is the right tool for sharing files across several machines. And structure your files according to your file system's limits - like number of files per directory and read/write capacity.

And a single database will be good enough for at least a small cluster, as long as you are not killing it with big binary blobs.

If in doubt, buy more ram ;) Especially the thumbnails are very cachable and will give good performance also in Tomcat - if you are not familiar with multi-threading, find a cache at google. Also cache the entities naturally, not only the files.

查看更多
时光不老,我们不散
3楼-- · 2019-03-21 17:39

You might want to use a (private) FTP server for this. Your EJB beans can contact this server for storing and retrieving files.

There are various libraries in Java for accessing FTP servers. Specifically well suited for use in an EJB environment would be JCA based FTP connectors, but 'normal' ones will usually work fine too.

查看更多
不美不萌又怎样
4楼-- · 2019-03-21 17:45

I'd suggest you to stick to your current solution. Ftp access (if needed for purposes other than just keeping files together) can be build on top of your ejb layer. Displaying images stored in DB is not a problem, simple servlet will do the trick.

查看更多
对你真心纯属浪费
5楼-- · 2019-03-21 17:46

You can :

  1. Create a WebDAV based file share. This can be done by using many libraries available for Java or other languages. One such library is : http://milton.ettrema.com/index.html
  2. All EJB instances can read /write images from this file share. They would need to use WebDav client libraries
  3. DO setup backups of directories behind this file share
查看更多
狗以群分
6楼-- · 2019-03-21 17:47

If you can make reasonable assumptions about "where" your EJB instance is, direct handling of a file is no problem. In your case (since you want to have files) I would read the image into a local temp folder and upload it to a remote destination.

A possible way to do that is http://txconnect.sourceforge.net/ a JCA Transaction Adapter that handes (among others) ftp connections. Configure the factory in xml and inject the connection into your bean and you have ready to go.

Depending on your Application server there might be a special connector available (f.e.: Oracle or IBM systems)

查看更多
做自己的国王
7楼-- · 2019-03-21 18:00

You can also look into using a clustered file system. RedHat Global File System and Symantec's Veritas Clustered File System are two I have experience with. These products allow you to mount the same file system across several servers for read/write access. All your application sees is just another directory. If your environment already has the requisite components (SAN and a good Sys Admin), this might be the best performing solution in a lot of use cases.

However, there are drawbacks to this approach:

  1. You shift complexity from your app to the OS. These products aren't trivial to set up.
  2. Scalability might become an issue if you have a large server farm. And when scaling problems arise finding the bottleneck is not as straight forward as arjan's ftp solution.
  3. You need a SAN.
查看更多
登录 后发表回答