Collections.sublist() not serializable

Ran into a strange serialization issue when trying to add a Java class to memcached…

Caused by:

java.io.NotSerializableException: java.util.RandomAccessSubList
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
net.spy.memcached.transcoders.BaseSerializingTranscoder.serialize(
BaseSerializingTranscoder.java:105)

The problem was with a class member that was initialized with Collections.sublist() method. This method returns an instance of java.util.RandomAccessSubList which is not serializable.

The solution was to wrap initialize the list as an ArrayList.

new ArrayList<String>(biglist.subList(2, 4))
Advertisements

Installing memcached on Mac OS X

Easiest way to install memcached on Mac OS X is using MacPorts (port) http://www.macports.org/

According to the MacPorts website…

The MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the Mac OS X operating system. To that end we provide the command-line driven MacPorts software package under a BSD License, and through it easy access to thousands of ports that greatly simplify the task of compiling and installing open-source software on your Mac.

The easiest way to install MacPorts on a Mac OS X system is by downloading the dmg for Lion, Snow Leopard or Leopard and running the system’s Installer by double-clicking on the pkg contained therein, following the on-screen instructions until completion. http://www.macports.org/install.php

Now, to install memcached, type the following command

sudo port install memcached

Memcached Server can be started by running the following command

memcached -vv

To run memcached as a daemon

memcached -d

These instructions have been tested on Mac OS X 10.6.8 (Snow Leopard)

Using Memcached with Java on Windows

Windows Memcached software is available on this page: http://code.jellycan.com/memcached/

Download the Win32 binary zip file. Unzip the file (memcached.exe) in a directory of choice and then do the following to install memcached as a service.

c:/bin/memcached.exe  -d install

At this point the service is installed, it can started or stopped using the following commands

c:/bin/memcached.exe -d  start
c:/bin/memcached.exe -d  stop

By default, the memcached service has a limit of 64MB. If that is not enough, you can increase by tweaking the Windows Registry. Open the following entry.

HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/memcached Server

Locate the entry called ImagePath. To increase the limit to 128MB set the value to…

“c:/bin/memcached.exe” -d runservice -m 512

A good Java client for memcached is spymemcached. Download the jar file from here: http://code.google.com/p/spymemcached/

// Get a memcached client connected to the local server
MemcachedClient c = new MemcachedClient(AddrUtil.getAddresses("127.0.0.1:11211"));
// Store a value (async) for one hour
c.set("someKey", 3600, someObject);
// Retrieve a value (synchronously).
Object myObject= c.get("someKey");