Problem using SpyMemCached in Java (non-serializable object when adding it to spymemcache)

843 views
Skip to first unread message

mvdput7685

unread,
Oct 21, 2009, 1:14:00 PM10/21/09
to spymemcached
Hello,

I'm trying write an object to spymemcache in Java but I keep getting
an "non-serializable object" error when trying to add an object to the
memcacheclient. The connection to spymemcache is wokring and I can see
the "connections" and "gets" coming in.

What am i doing wrong?
See my code below (I just simplified it to see if the error had
something to do with the original object (a generic List) I want to
store)

/** my object to store **/
public class TrainingList implements Serializable {
private static final long serialVersionUID = 1L;

private String trainingList;

public TrainingList(String myList) {
trainingList = myList;
}

public String getTrainingList() {
return trainingList;
}
}

/** method to call add to cache function **/
public void AddToCache() {
TrainingList myList = new TrainingList("test");
writeTrainingToCache(myList);
}

/** method to add to cache **/
private void writeTrainingToCache(TrainingList myList) {
try {
// Get a memcached client connected to one server
MemcachedClient myCacheClient = new MemcachedClient(new
InetSocketAddress(getMemCacheHostName(), 11211));
// add the new object
myCacheClient.add(cacheKey, 21600, myList);
LOG.log(Level.INFO, "Trainings written to MemCache.");
} catch (Exception e) {
// handle exception
LOG.log(Level.SEVERE, "Error writing trainingObject to Cache. " +
e.getMessage() );
}
}

Boris Partensky

unread,
Oct 21, 2009, 2:28:17 PM10/21/09
to spymem...@googlegroups.com
I think it's because you are missing a no arg constructor.
--
--Boris

mvdput7685

unread,
Oct 26, 2009, 9:20:31 AM10/26/09
to spymemcached
I've added an empty constructor in my class, but I get the same "non-
serializable object" errors when trying to add an object to the spy
memcache.
See my modified class below:

public class TrainingList implements Serializable {
private static final long serialVersionUID = 1L;
private String trainingList;

public TrainingList()
{
// empty constructor }
> --Boris- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

mvdput7685

unread,
Oct 26, 2009, 9:21:51 AM10/26/09
to spymemcached
public class TrainingList implements Serializable {
private static final long serialVersionUID = 1L;
private String trainingList;

public TrainingList()
{
// do nothing
> > - Tekst uit oorspronkelijk bericht weergeven -- Tekst uit oorspronkelijk bericht niet weergeven -

Boris Partensky

unread,
Oct 26, 2009, 9:47:36 AM10/26/09
to spymem...@googlegroups.com
Hm, as far as I can see there is no reason it should complain. Can you paste your entire code, including import statements and test case?
--
--Boris

mvdput7685

unread,
Oct 26, 2009, 10:05:50 AM10/26/09
to spymemcached
Here's the code. The total class is rather big (about a 1000 lines) so
I've only copied the relevant lines.

/***** START ******/
package nl.colours.sn.product.productdataexchange.service;

import net.spy.memcached.*;

/**
* Implementation of the ProductDataExchange service component.
*/
public class ProductDataExchangeServiceImpl extends
SimpleServiceComponent implements ProductDataExchangeService,
ConfigurationManagementListener {


public class TrainingList implements Serializable {
private static final long serialVersionUID = 1L;
private String trainingList;

public TrainingList()
{
// empty constructor
}

public TrainingList(String myList) {
trainingList = myList;
}

public String getTrainingList() {
return trainingList;
}
}

public List<Training> getAllTrainings() {
try {
LOG.log(Level.INFO, "Get all tranining (before cache.)");
synchronized (myUpdatingCachedTrainings) {
if(myCachedTrainings==null ||
myUpdatingCachedTrainings || (myCachedTrainingsLastUpdate != null &&
(new Date().getTime() - myCachedTrainingsLastUpdate.getTime()) >=
getUpdateInterval() )){
myCachedTrainings = getTrainingExportManager
().getAll();
TrainingList myList = new TrainingList("test");
writeTrainingToCache(myList);
myUpdatingCachedTrainings=false;
myCachedTrainingsLastUpdate = new Date();

LOG.log(Level.INFO, "Trainings cache was
updated.");
}
}
try {
Object trainingList = getTrainingFromCache();
if (trainingList != null)
myCachedTrainings = (List<Training>)trainingList;
} catch (Exception e) {
// handle exception
LOG.log(Level.SEVERE, "Error casting cache object to
List<Training>");
}

return myCachedTrainings;
} catch (ProductDataExchangeException e) {
LOG.log(Level.SEVERE, "Error during retrieving all
products [" + e.getMessage() + "]");
return Collections.emptyList();
}
}

private String getMemCacheHostName() {
try {
String cacheHostName =
myConfigurationManagement.getParameter
(WCBConstants.CONFIG_SET_NAME + "."
+ WCBConstants.MEMCACHE_SERVER_IP);
return cacheHostName;
} catch (ConfigurationManagementException e) {
LOG.log(Level.WARNING, "Could not find parameter '" +
WCBConstants.MEMCACHE_SERVER_IP
+ "' in configuration set '" +
WCBConstants.CONFIG_SET_NAME + "'", e);
}
return WCBConstants.DEFAULT_MEMCACHE_SERVER_IP;
}

private void writeTrainingToCache(TrainingList myList) {
try {
// Get a memcached client connected to one server
MemcachedClient myCacheClient = new MemcachedClient(new
InetSocketAddress(getMemCacheHostName(), 11211));
// add the new object
myCacheClient.add(cacheKey, 21600, myList);
LOG.log(Level.INFO, "Trainings written to MemCache.");
} catch (Exception e) {
// handle exception
LOG.log(Level.SEVERE, "Error writing trainingObject to Cache. " +
e.getMessage() );
}
}

private Object getTrainingFromCache() {
Object myObject = null;

try {
// Get a memcached client connected to one server
MemcachedClient myCacheClient = new MemcachedClient(new
InetSocketAddress(getMemCacheHostName(), 11211));
// Try to get a value, for up to 5 seconds, and cancel if it
doesn't return
Future<Object> f = myCacheClient.asyncGet(cacheKey);
try {
myObject = f.get(5, TimeUnit.SECONDS);
LOG.log(Level.INFO, "Trainings read from MemCache.");

} catch (TimeoutException e) {
// Since we don't need this, go ahead and cancel the operation.
This
// is not strictly necessary, but it'll save some work on the
// server.
f.cancel(false);
// Do other timeout related stuff
LOG.log(Level.SEVERE, "Error timeout expired during retrieving
trainingObject Cache.");
}
} catch (Exception e) {
// handle exception
LOG.log(Level.SEVERE, "Error during retrieving trainingObject
Cache.");
}

return myObject;
}

}
/***** END ******/

Boris Partensky

unread,
Oct 26, 2009, 10:11:36 AM10/26/09
to spymem...@googlegroups.com
Wait, is TrainingList an inner class? In this case the outer class has to be serializable as well since the child class holds compiler generated reference to the outer class. Make sure that all the members of TrainingList are serializable (implicit and explicit).
--
--Boris

mvdput7685

unread,
Oct 26, 2009, 10:37:06 AM10/26/09
to spymemcached
hm, that could be the point. I will modify and test this asap and post
the results here.

mvdput7685

unread,
Oct 26, 2009, 11:42:54 AM10/26/09
to spymemcached
I made the class "static". That did the trick. :)

Now I can add my original class and object and I guess it should start
to work now.
> ...
>
> meer lezen »- Tekst uit oorspronkelijk bericht niet weergeven -

Liu Frank

unread,
Jul 21, 2017, 12:43:26 AM7/21/17
to spymemcached, mar...@parcae.nl
Thanks for this Post. It solves me the problem that bothers me so long. 
Reply all
Reply to author
Forward
0 new messages