2010-07-09 88 views
8

大家好我正在使用memcache,并且当我编译此代码时,出现以下错误。处理Memcache时出现不可序列化的对象错误

2010-07-09 10:35:53.499 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue 
2010-07-09 10:35:53.520 INFO net.spy.memcached.MemcachedConnection: Connection state changed for [email protected] 
Exception in thread "main" java.lang.IllegalArgumentException: Non-serializable object 
at net.spy.memcached.transcoders.BaseSerializingTranscoder.serialize(BaseSerializingTranscoder.java:86) 
at net.spy.memcached.transcoders.SerializingTranscoder.encode(SerializingTranscoder.java:135) 
at net.spy.memcached.MemcachedClient.asyncStore(MemcachedClient.java:301) 
at net.spy.memcached.MemcachedClient.set(MemcachedClient.java:691) 
at def.Tweet.main(Tweet.java:23) 
Caused by: java.io.NotSerializableException: def.User 
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1173) 
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:343) 
at net.spy.memcached.transcoders.BaseSerializingTranscoder.serialize(BaseSerializingTranscoder.java:81) 
... 4 more 

我设法与转换用户类的toString修复它,但我不希望使用的toString method.I只是想用我的类添加并获得mwethods。我该如何解决这个问题? 这是我使用的代码。

package def; 

import java.io.IOException; 
import java.io.Serializable; 
import java.net.InetSocketAddress; 

import net.spy.memcached.MemcachedClient; 

public class Tweet { 
    private static User user; 
    private static Message message; 
    private static Followers follower; 

public static void main(String[] args) throws Exception { 
try{ 
    user = new User(1,"Mehmet Özer","asd","127.0.0.1","True","Onine"); 
    //String deneme = user.toString(); 
    MemcachedClient c=new MemcachedClient(new InetSocketAddress("localhost", 11211)); 


    // Store a value (async) for one hour 

    c.set(user.userKey, 3600, user); 
    System.out.println(c.get(user.userKey)); 

    } 
    catch(IOException ex) { 
     ex.printStackTrace(); 
    } 
} 
} 

对不起,我忘了User类,我也给你用户类,以帮助我更好。

public class User { 
    public static String userKey = "UserDB"; 
    public static int userID ; 
    public static String userName; 
    public static String password; 
    public static String ipAddress; 
    public static String isOnline; 
    public static String lastActive; 

    public User(int userID, String userName, String password, String ipAddress,String isOnline,String lastActive) 
    { 
     this.userID = userID; 
     this.userName = userName; 
     this.password = password; 
     this.ipAddress = ipAddress; 
     this.isOnline = isOnline; 
     this.lastActive = lastActive; 

    } 
    @Override 
    public String toString() { 
     System.out.println(this.userID); 
     System.out.println(this.userName); 
     System.out.println(this.password); 
     System.out.println(this.ipAddress); 
     System.out.println(this.isOnline); 
     System.out.println(this.lastActive); 

     return super.toString(); 
    } 


    } 

回答

12

Memcache不知道如何序列化您的对象。如果您需要更多地控制(反)序列化过程,您需要实现Serializable让Java处理序列化,或者Externalizable

+0

我需要在可序列化的方法中实现什么? – mehmetozer 2010-07-09 08:02:55

+3

您需要实现'Serializable'接口:'public class User implements Serializable {}'。该接口只是一个标记,它告诉序列化机制“User”类可以被序列化。实际的序列化是自动完成的,所以你不必实现任何方法。 – 2010-07-09 08:26:31

+0

此外,应用toString()首先工作的原因是因为String是可序列化的,即它可以转换为字节码并返回。 – 2016-05-31 15:47:50

3

由于问题修改而更新。

因此,您的用户类必须实现Serializable。希望这与写作一样简单

public class User implements Serializable { 
... 
} 

由于Serializable不包含任何方法。

+0

对不起,我错过了class.I添加了用户类,如你所说。 – mehmetozer 2010-07-09 08:02:19

0

此问题即将出现,因为JVM无法正确序列化对象,因为模式中存在冲突。在我的情况下,某些类的串行版本ID默认定义为1L。这些课程是相互合作的。我分配了一些独特的自定义值。我的问题已解决。

相关问题