How to send inputStream object to Java EJBean?

I have a Java client for a bean session, I want to send it an inputStream as follows:

Note. I work with EJB 3.0

public class SenderSimulator { public static void main(String[] arg){ Properties p = new Properties(); p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); p.put("java.naming.provider.url", "jnp://localhost:1099"); p.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); try { Context context = new InitialContext(p); RogersBatchImporter bean = (RogersBatchImporter)context.lookup("RogersImporterBean/remote"); InputStream in = new FileInputStream("filePath"); System.out.println("Result: " + bean.processBatch(in)); // line 29 } catch (NamingException e) { e.printStackTrace(); } catch (LogConfigurationException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } 

}

but this excludes the following:

 Exception in thread "main" java.lang.reflect.UndeclaredThrowableException at $Proxy0.processBatch(Unknown Source) at package.main(SenderSimulator.java:29) Caused by: java.rmi.MarshalException: Failed to communicate. Problem during marshalling/unmarshalling; nested exception is: java.io.NotSerializableException: java.io.FileInputStream at org.jboss.remoting.transport.socket.SocketClientInvoker.handleException(SocketClientInvoker.java:127) at org.jboss.remoting.transport.socket.MicroSocketClientInvoker.transport(MicroSocketClientInvoker.java:689) at org.jboss.remoting.MicroRemoteClientInvoker.invoke(MicroRemoteClientInvoker.java:122) at org.jboss.remoting.Client.invoke(Client.java:1634) at org.jboss.remoting.Client.invoke(Client.java:548) at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(InvokeRemoteInterceptor.java:62) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.tx.ClientTxPropagationInterceptor.invoke(ClientTxPropagationInterceptor.java:67) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.security.SecurityClientInterceptor.invoke(SecurityClientInterceptor.java:53) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:74) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:107) ... 2 more Caused by: java.io.NotSerializableException: java.io.FileInputStream at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156) at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1338) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1146) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326) at java.rmi.MarshalledObject.<init>(MarshalledObject.java:101) at org.jboss.aop.joinpoint.MethodInvocation.writeExternal(MethodInvocation.java:318) at java.io.ObjectOutputStream.writeExternalData(ObjectOutputStream.java:1421) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1390) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150) at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326) at org.jboss.remoting.serialization.impl.java.JavaSerializationManager.sendObjectVersion2_2(JavaSerializationManager.java:120) at org.jboss.remoting.serialization.impl.java.JavaSerializationManager.sendObject(JavaSerializationManager.java:95) at org.jboss.remoting.marshal.serializable.SerializableMarshaller.write(SerializableMarshaller.java:120) at org.jboss.remoting.transport.socket.MicroSocketClientInvoker.versionedWrite(MicroSocketClientInvoker.java:969) at org.jboss.remoting.transport.socket.MicroSocketClientInvoker.transport(MicroSocketClientInvoker.java:606) at org.jboss.remoting.MicroRemoteClientInvoker.invoke(MicroRemoteClientInvoker.java:122) at org.jboss.remoting.Client.invoke(Client.java:1634) at org.jboss.remoting.Client.invoke(Client.java:548) at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(InvokeRemoteInterceptor.java:62) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.tx.ClientTxPropagationInterceptor.invoke(ClientTxPropagationInterceptor.java:67) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.security.SecurityClientInterceptor.invoke(SecurityClientInterceptor.java:53) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:74) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:107) at $Proxy0.processBatch(Unknown Source) at com.cybersource.rogers.batch.request.SenderSimulator.main(SenderSimulator.java:29) at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(InvokeRemoteInterceptor.java:74) ... 10 more 
+4
source share
4 answers

JBoss has a serialized input stream class.

This works to test a bean that uses an InputStream through a compressed interface.

0
source

As others have pointed out, File , InputStream and many other IO-related objects cannot be serialized and therefore cannot be stretched over the connection between the client and server (without additional functions). In the context of J2EE, a container should terminate processes as quickly as possible in order to minimize resource use and allow parallel processing. Prior to Java NIO , I / O operations are usually blocked (waiting for data to be read or written), which causes threads to hang (i.e. temporarily stop at best, forever at worst).

I was working on a project when some of the developers, who were fairly new to Java, opened FTP connections to remote servers from EJBs - against my warnings and J2EE docs. When the remote server did not respond, the only way to unleash our server is to kill and restart it!

Therefore: capture the contents of the file in the client and send it over the connection as a large string or array of characters or bytes or something else. Thus, your EJB process will have all the data ready for processing.

If the amount of data is too large to do so in a reasonable amount of memory, this is not a viable solution. The recommended solution in this case would be for the client to write data to the database (possibly BLOB) and read the EJB from the database.

+11
source

If you use a remote EJB, then the Objects parameters are sorted and unmarshaled (converted from the object to a byte stream, sent, and then back to Object). This requires all your parameters to be Serializable . Since FileInputStream is not serializable, it will not work as a parameter. You will need to send the contents of the file to something like String (byte [] should also work, I suppose).

+3
source

For wire transfers, the function arguments in your bean session must be serialized . A stream cannot be serialized (i.e., it does not implement the Serializable interface), so you cannot transfer it to a remote bean.

To get the argument for the remote bean, you will need to use a serializable view.

0
source

All Articles