• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing/Upload file to 2 servers

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have 2 servers that are load balanced. When I upload a file, it is uploaded only to the server that processes the request. I want to upload a file to both the servers. Is that possible? If so, how?

See simple code below to write the file.


 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What we did when we had a similar requirement was not to store the files on either of the load-balanced server, but to store them on a completely separate drive. Then we configured the two servers to access that drive for shared files.
 
Pan Igor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can not use another server to store files. Any other way? How can I store files using server names? java.io.File does not have the API
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We faced a similar problem. The first thing we did was hide all File I/O behind an interface that had 2 methods: save and load. It's important that none of your code accesses the filesystem directly because you want this kind of file replication to be handled consistently.

The first implementation of this service will probably just do whatever you are doing now, namely just saving or reading from local files. This step is basically just a refactoring of your existing code.

The next step is to write another implementation that will handle the file replication for you. In our code, the save method did the following:

1) Save the data to the local file system.
2) Write the file's checksum into a database somewhere (I'll explain later).
3) Attempt to copy (in parallel) the file to other servers in the cluster. We defined which servers belonged in each cluster through a property file.

On Windows, you can access another computer's files by doing something like: \\remote_machine\folder1\file.txt or something like that. Not entirely sure how to do it on Linux but I imagine it can't be too hard.

Finally, whenever a piece of the code needed to read a file, it calls the load method. This method first checks the local file system. If the file's there, great, if not, attempt to find it on one of the other servers in the cluster. We used the checksum here to make sure the file is valid; it's possible it might be corrupted. If it's corrupted, we move on to the next server. If no valid copy of the file exists, you can throw a FileNotFoundException or something similar.
 
reply
    Bookmark Topic Watch Topic
  • New Topic