pass object to another JVM using serialization - same Java version and jars (both running our app)

http, jakarta-ee, java, serialization

Solution

You need not convert to string. You can post the binary data straight to the servlet, for example by creating an ObjectOutputStream on top of a HttpUrlConnection's outputstream. Set the request method to POST.

The servlet handling the post can deserialize from an ObjectStream created from the HttpServletRequest's ServletInputStream.

I'd recommend JAXB any time over binary serialization, though. The frameworks are not only great for interoperability, they also speed up development and create more robust solutions.

The advantages I see are way better tooling, type safety, and code generation, keeping your options open so you can call your code from another version or another language, and easier debugging. Don't underestimate the cost of hard to solve bugs caused by accidentally sending the wrong type or doubly escaped data to the servlet. I'd expect the performance benefits to be too small to compensate for this.

Problem

Updates: For now using a Map. Class that wants to send something to other instance sends the object, the routing string. Use an object stream, use Java serializable to write the object to servlet. Write String first and then the object. Receiving servlet wraps input stream around a ObjectInputStream. Reads string first and then the Object. Routing string decides were it goes. A more generic way might have been to send a class name and its declared method or a Spring bean name, but this was enough for us. Original question Know the basic way but want details of steps. Also know I can use Jaxb or RMI or EJB ... but would like to do this using pure serialization to a bytearray and then encode that send it from servlet 1 in jvm 1 to servlet 2 in jvm 2 (two app server instances in same LAN, same java versions and jars set up in both J2EE apps) Basic steps are (Approcah 1) :- serialize any Serializable object to a byte array and make a string. Exact code see below Base64 output of 1. Is it required to base 64 or can skip step 2? use java.util.URLEncode.encode to encode the string use apache http components or URL class to send from servlet 1 to 2 after naming params on Servlet 2 J2EE framework would have already URLDecoced it, now just do reverse steps and cast to object according to param name. Since both are our apps we would know the param name to type / class mapping. Basically looking for the fastest & most convenient way of sending objects between JVMs. Example : POJO class to send ``` package tst.ser; import java.io.Serializable; public class Bean1 implements Serializable { /** * make it 2 if add something without default handling */ private static final long serialVersionUID = 1L; private String s; public String getS() { return s; } public void setS(String s) { this.s = s; } } ``` * Utility * ``` package tst.ser; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.URLEncoder; public class SerUtl { public static String serialize(Object o) { String s = null; ObjectOutputStream os = null; try { os = new ObjectOutputStream(new ByteArrayOutputStream()); os.writeObject(o); s = BAse64.encode(os.toByeArray()); //s = URLEncoder.encode(s, "UTF-8");//keep this for sending part } catch (Exception e) { // TODO: logger e.printStackTrace(); return null; } finally { // close OS but is in RAM try { os.close();// not required in RAM } catch (Exception e2) {// TODO: handle exception logger } os = null; } return s; } public static Object deserialize(String s) { Object o = null; ObjectInputStream is = null; try { // do base 64 decode if done in serialize is = new ObjectInputStream(new ByteArrayInputStream( Base64.decode(s))); o = is.readObject(); } catch (Exception e) { // TODO: logger e.printStackTrace(); return null; } finally { // close OS but is in RAM try { is.close();// not required in RAM } catch (Exception e2) {// TODO: handle exception logger } is = null; } return o; } } ``` **** sample sending servlet *** ``` Bean1 b = new Bean1(); b.setS("asdd"); String s = SerUtl.serialize(b); //do UrlEncode.encode here if sending lib does not. HttpParam p = new HttpParam ("bean1", s); //http components send obj ``` **** sample receiving servlet *** ``` String s = request.getParameter("bean1"); Bean1 b1 = (Beean1)SerUtl.deserialize(s); ```

Original source