[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Uploading files (using a java client, not jsp, etc.) - [org.

IceBearOZ do-not-reply at jboss.com
Thu Nov 9 12:10:53 EST 2006


Hi Together,

First of all I was not sure where to put this ? in the beginners section (cause I am using Jboss since 2,5 months), or in the ejb3 section according to the error message. So hopefully I am right here.

What I want to do:
I want to upload a file from a java client to JBoss:

I read in several forums that ejb actually does not support file upload (apart from uploading files via servlets, jsf and HTTP, etc.). Later I found a workaround (putting my files as portions of bytes to server).

So I programmed the class (client) and a session bean below and everything works fine, except:

When I declare the session bean as @Stateful I get for every time the write()-method is called the message:

DEBUG [org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor] ++++ LongLivedSessionPropagationInterceptor

My file is 252kb and I get the message 252 times in my server log plus one time when I call open() and one time when calling close():


  | 2006-11-09 17:03:22,249 DEBUG [org.jboss.remoting.transport.socket.ServerThread] WAKEUP in SERVER THREAD
  | 2006-11-09 17:03:22,249 DEBUG [org.jboss.remoting.transport.socket.ServerThread] beginning dorun
  | 2006-11-09 17:03:22,327 DEBUG [org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor] ++++ LongLivedSessionPropagationInterceptor
  | 2006-11-09 17:03:22,327 INFO  [test.ejb.appclientsync.sessionbeans.RemoteStreamBean] Open remote stream: successful.
  | 2006-11-09 17:03:22,343 DEBUG [org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor] ++++ LongLivedSessionPropagationInterceptor
  | 2006-11-09 17:03:22,343 DEBUG [org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor] ++++ LongLivedSessionPropagationInterceptor
  | ......
  | ....
  | ..
  | total 252
  | ..
  | ....
  | ......
  | 2006-11-09 17:03:22,905 DEBUG [org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor] ++++ LongLivedSessionPropagationInterceptor
  | 2006-11-09 17:03:22,905 INFO  [test.ejb.appclientsync.sessionbeans.RemoteStreamBean] Closing remote stream: successful
  | 2006-11-09 17:03:22,921 DEBUG [org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor] ++++ LongLivedSessionPropagationInterceptor
  | 2006-11-09 17:03:22,921 INFO  [test.ejb.appclientsync.sessionbeans.RemoteStreamBean] SourceFileChecksum vs. RemoteFileChecksum: 748869239 / 748869239
  | 2006-11-09 17:03:22,921 INFO  [test.ejb.appclientsync.sessionbeans.RemoteStreamBean] File is OK
  | 2006-11-09 17:03:22,952 DEBUG [org.jboss.remoting.transport.socket.ServerThread] begin thread wait
  | 

What I am doing wrong rather what do I have to do to do it right?

When declaring the session bean as @Stateless everything is fine:

  | 2006-11-09 17:01:01,499 DEBUG [org.jboss.remoting.transport.socket.ServerThread] WAKEUP in SERVER THREAD
  | 2006-11-09 17:01:01,499 DEBUG [org.jboss.remoting.transport.socket.ServerThread] beginning dorun
  | 2006-11-09 17:01:01,515 INFO  [test.ejb.appclientsync.sessionbeans.RemoteStreamBean] Open remote stream: successful.
  | 2006-11-09 17:01:02,062 INFO  [test.ejb.appclientsync.sessionbeans.RemoteStreamBean] Closing remote stream: successful
  | 2006-11-09 17:01:02,062 INFO  [test.ejb.appclientsync.sessionbeans.RemoteStreamBean] SourceFileChecksum vs. RemoteFileChecksum: 748869239 / 748869239
  | 2006-11-09 17:01:02,062 INFO  [test.ejb.appclientsync.sessionbeans.RemoteStreamBean] File is OK
  | 2006-11-09 17:01:02,109 DEBUG [org.jboss.remoting.transport.socket.ServerThread] begin thread wait
  | 

This is my session bean:

  | package test.ejb.appclientsync.sessionbeans;
  | 
  | import java.io.FileOutputStream;
  | import java.io.IOException;
  | import java.util.zip.Adler32;
  | import java.util.zip.CheckedOutputStream;
  | 
  | import javax.ejb.Stateful;
  | 
  | import org.apache.log4j.Logger;
  |  
  | @Stateful
  | public class RemoteStreamBean implements RemoteStreamBeanRemote { 
  | 	
  | 	public static Logger logger = Logger.getLogger(RemoteStreamBean.class);
  | 		
  | 	public static final String RemoteJNDIName = 
  | 			RemoteStreamBean.class.getSimpleName() + "/remote";
  | 	public static final String LocalJNDIName = 
  | 			RemoteStreamBean.class.getSimpleName() + "/local";
  | 
  | 	private static CheckedOutputStream cos;
  | 	private static FileOutputStream fos;
  | 	
  | 	private static long checksum;
  | 
  | 	public RemoteStreamBean() {
  | 		super();
  | 	}
  | 
  | 	public void open() {
  | 		try {
  | 			fos = new FileOutputStream("C:/test/remote/testfile.txt");
  | 			cos = new CheckedOutputStream(fos, new Adler32());
  | 			logger.info("Open remote stream: successful.");
  | 		} catch (IOException ioe) {
  | 			logger.info("Failure during open()");
  | 		}
  | 	
  | 	}
  | 	
  | 	public boolean write(byte[] data, int off, int len) {
  | 		try {
  | 			cos.write(data, off, len);
  | 			cos.flush();
  | 			return true;
  | 		} catch (Exception IOException) {
  | 			logger.info("Failure during write()...");
  | 			//cleanReset(true);
  | 			return false;
  | 		}
  | 	}
  | 
  | 	public boolean close() {
  | 		try {
  | 			checksum = cos.getChecksum().getValue();
  | 			cos.close();
  | 			logger.info("Closing remote stream: successful");
  | 			return true;
  | 		} catch (IOException ioe) {
  | 			logger.info("Failure during close()");
  | 			return false;
  | 		}
  | 	}
  | 	
  | 	public boolean success(long sourceFileChecksum) {
  | 	
  | 		logger.info("SourceFileChecksum vs. RemoteFileChecksum: " + 
  | 				sourceFileChecksum + " / " + checksum);
  | 		
  | 		if(sourceFileChecksum != checksum) {
  | 			logger.info("Transmitted file is courrupt");
  | 			return false;
  | 		} else {
  | 			logger.info("File is OK");
  | 			return true;
  | 			
  | 		}
  | 	}
  | 	
  | }//end class
  | 

My Interface:

  | package test.ejb.appclientsync.sessionbeans;
  | 
  | import javax.ejb.Remote;
  | 
  | @Remote
  | public interface RemoteStreamBeanRemote {
  | 
  | 	public void open();
  | 	
  | 	public boolean write(byte[] data, int off, int len);
  | 	
  | 	public boolean close();
  | 	
  | 	public boolean success(long sourceFileChecksum);
  | 	
  | }

Client (started via a simple class containing main()-method only):

  | package test.syncbaker.appclient.syncmode;
  | 
  | import java.io.FileInputStream;
  | import java.io.FileNotFoundException;
  | import java.io.IOException;
  | import java.util.zip.Adler32;
  | import java.util.zip.CheckedInputStream;
  | 
  | import javax.naming.Context;
  | import javax.naming.InitialContext;
  | import javax.naming.NamingException;
  | 
  | import org.apache.log4j.Logger;
  | 
  | import test.ejb.appclientsync.sessionbeans.RemoteStreamBean;
  | import test.appclientsync.sessionbeans.RemoteStreamBeanRemote;
  | 
  | public class FileUpload {
  | 
  | 	public Logger logger = Logger.getLogger(FileUpload.class);
  | 	
  | 	private Context context;
  | 	private static RemoteStreamBeanRemote rfos;
  | 	
  | 	private FileInputStream fis;
  | 	private CheckedInputStream cis;
  | 	
  | 	public FileUpload() {
  | 		super();
  | 		loadRemoteFileOutputStream();
  | 	}
  | 	
  | 	private void loadRemoteFileOutputStream() {
  | 		try {
  | 			context = new InitialContext();
  | 			rfos = (RemoteStreamBeanRemote)
  | 				    context.lookup(RemoteStreamBean.RemoteJNDIName);
  | 		} 
  | 		catch (NamingException ne) {
  | 			logger.error("Unable to load (connect) remote service.");
  | 			logger.error(ne);
  | 			System.exit(-1);
  | 		}
  | 	}
  | 	
  | 	public void uploadFile() {
  | 		
  | 		/*
  | 		 * Checksum
  | 		 */
  | 		long checksum = 0;
  | 		
  | 		/*
  | 		 * Open RemoteFileOutputStream 
  | 		 */
  | 		rfos.open();	
  | 			
  | 		try {
  | 			
  | 			/*
  | 			 * Open local InputStreams
  | 			 */
  | 			fis = new FileInputStream("c:/test/source/testfile.txt");
  | 			cis = new CheckedInputStream(fis, new Adler32());
  | 	        
  | 			/*
  | 			 * Transfer file
  | 			 */ 
  | 	        byte[] buf = new byte[1024];
  | 	        int len;
  | 	        while ((len = cis.read(buf)) > 0) {
  | 	        	
  | 	        	/*
  | 	        	 * Transmit file
  | 	        	 */
  | 	        	boolean transmitState = rfos.write(buf, 0, len);
  | 
  | 	        	/*
  | 	        	 * success?
  | 	        	 */
  | 	        	if(!transmitState) {
  | 	        		logger.warn("Warning: Failure during uploading");
  | 	        		
  | 	        		throw new IOException();
  | 	  
  | 	        	}
  | 	        	
  | 	        }// end while transmit
  | 	        
  | 	        /*
  | 	         * Get checksum of stream
  | 	         */
  | 	        checksum = cis.getChecksum().getValue();
  | 	        
  | 	        /*
  | 	         * Close InputStream
  | 	         */
  | 	        cis.close();
  | 	        
  | 			/*
  | 			 * Close RemoteFileOutputStream.
  | 			 */
  | 	        boolean closingState = rfos.close();
  | 	        if(!closingState) {
  | 	    		logger.warn("Failure during closing remote stream.");
  | 	    		throw new IOException();
  | 	        }
  | 	        logger.info("Closing remote stream: successful");
  | 	        
  | 	        /*
  | 	         * Finally check success of transmission.
  | 	         */
  | 	        boolean success = rfos.success(checksum);
  | 	        if(!success) {
  | 	        	logger.warn("File uploaded is corrupt");
  | 	        	throw new IOException();
  | 	        } else {
  | 	        	logger.debug("File upload complete.");
  | 	        }
  | 	        
  | 	    /*
  | 	     * Handel Exceptions
  | 	     */    
  | 		} catch (FileNotFoundException fnfe) {
  | 			fnfe.printStackTrace();
  | 		} catch (IOException ioe) {
  | 			ioe.printStackTrace();
  | 		}
  |         
  | 	}
  | 	
  | }
  | 

Thanks in advance.

Regards
Rene

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3984597#3984597

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3984597



More information about the jboss-user mailing list