所以我觉得我对memcached的工作原理有点困惑。我正在使用不同的框架开发相同的API,所有框架都包含memcached(java版本的spymemcached)。当我在我的第一个框架上设置memcached时,它按预期工作。现在我正在尝试将它合并到我正在使用的第二个框架上,我继续得到一个ClassNotFoundException,它试图在我使用的第一个框架中找到一个类,因此不会缓存任何东西。也许有人可以启发我的逻辑中的缺陷。这是两个类(它们是相同的,因为我在不同的平台上构建相同的API),core.Party
类在第一个类中(在运行相应的应用程序时有效)pojo.PojoParty
是我正在使用的一个和第二个框架似乎没有连接到memcached。
以下是发生它的类的构造函数:
package resources;
import health.TemplateHealthCheck;
import java.net.InetSocketAddress;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.ResultSetMetaData;
import java.util.HashMap;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Counter;
import com.codahale.metrics.RatioGauge;
import contextListener.MyAdminServletContextListener;
import pojo.PartyPojo;
import net.spy.memcached.MemcachedClient;
public class DBConnection {
public static Connection con;
private static ResultSet resultSet;
private static PreparedStatement ps;
private static ResultSetMetaData meta;
private static HashMap<String,PartyPojo> map;
private static DBConnection connection;
private static PartyPojo party;
private static final InetSocketAddress isa= new InetSocketAddress("atom1.cisco.com",11211);
private static MemcachedClient memcache;
private static Meter hits = MyAdminServletContextListener.registry.meter(MetricRegistry.name("Meter",
"get-hits"));
private static Meter calls = MyAdminServletContextListener.registry.meter(MetricRegistry.name("Meter",
"get-calls"));
private static Counter evictions = MyAdminServletContextListener.registry.counter(MetricRegistry.name("Counter",
"Memcache-Evictions"));
private static int getHits,getCalls;
private final static int MAX_MEMCACHED_EXPIRATION=2505600;
private DBConnection()
{
try
{
map = new HashMap<String,PartyPojo>();
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://atom3.cisco.com:3306/reddb", "redteam",
"redteam");
//initialize memcache
memcache = new MemcachedClient(isa); //this line produces error
//this is just a integer variable I was using to make sure my Meters were measuring correctly
getHits = Integer.valueOf(DBConnection.memcache.getStats().get(DBConnection.isa).
get("get_hits"));
getCalls = Integer.valueOf(DBConnection.memcache.getStats().get(DBConnection.isa).
get("cmd_get"));
metricsRegistry();
}
catch (Exception e)
{
e.printStackTrace();
}
}
这是它试图达到的类,它不是第二个框架的一部分(这是第一个框架中的pojo类):
package core;
import java.io.Serializable;
import java.util.HashMap;
public class Party implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
HashMap <String,String> partyInfo = new HashMap<String,String>();
public HashMap<String,String> getPartyInfo() throws Exception
{
return partyInfo;
}
}
这是相同的类,但在项目中我正在尝试运行
package pojo;
import java.io.Serializable;
import java.util.HashMap;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement(name="Party")
@XmlAccessorType(XmlAccessType.FIELD)
public class PartyPojo implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4892745279086305480L;
/**
*
*/
/**
*
*/
@XmlPath(".")
@XmlJavaTypeAdapter(MapAdapter.class)
HashMap <String,String> partyInfo = new HashMap<String,String>();
public HashMap<String,String> getPartyInfo() throws Exception
{
return partyInfo;
}
}
这是堆栈跟踪:
2013-08-05 15:54:39.810 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=atom1.cisco.com/173.36.62.251:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-08-05 15:54:39.847 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@529762a
2013-08-05 15:54:40.120 WARN net.spy.memcached.transcoders.SerializingTranscoder: Caught CNFE decoding 434 bytes of data
java.lang.ClassNotFoundException: core.Party
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at net.spy.memcached.transcoders.BaseSerializingTranscoder.deserialize(BaseSerializingTranscoder.java:129)
at net.spy.memcached.transcoders.SerializingTranscoder.decode(SerializingTranscoder.java:88)
at net.spy.memcached.transcoders.TranscodeService$1.call(TranscodeService.java:63)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at net.spy.memcached.transcoders.TranscodeService$Task.run(TranscodeService.java:110)
at net.spy.memcached.transcoders.TranscodeService$Task.get(TranscodeService.java:96)
at net.spy.memcached.internal.GetFuture.get(GetFuture.java:63)
at net.spy.memcached.MemcachedClient.get(MemcachedClient.java:997)
at net.spy.memcached.MemcachedClient.get(MemcachedClient.java:1018)
at resources.DBConnection.readOneParty(DBConnection.java:114)
at resources.OnePartyResource.getParty(OnePartyResource.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:134)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:698)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:505)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:211)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1094)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:432)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1028)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:445)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:267)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:224)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:601)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:532)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:1)
我想通了,答案很尴尬。因为我急于在不同的框架上完成代码,所以我基本上将代码从一个复制并粘贴到另一个框架。基本上发生的事情是存储来自第一个API罚款的值;但是,当我尝试使用其他API查询这些相同的值时,它试图将这些值存储在同一密钥位置的memcache中。因此,为什么它找不到对象,它是在错误的项目!所以我基本上为一个程序附加了字符串键,以便它能正确返回。
显然是一个令人尴尬的错误,但我会把它归结为我第一次在多个程序中使用任何类型的缓存系统。
答案 1 :(得分:0)
当memcached
(或任何序列化程序)尝试从序列化表单重新构建对象时,它必须能够访问相应的类。反序列化方面没有core.Party
可用,因此memcached
没有传入数据流的Java表示。
当你谈到“不同的平台”时,你似乎误解了Java库的工作方式。既然您在双方都使用Java,那么您不应该拥有该类的两个“副本”,您应该只使用一个单独的类。如果存在一些共享代码(例如Party
),但某些其他代码不应在两个发行版之间共享,请将Party
放入两个项目包含的库jar中。 Maven和其他构建管理系统使这很容易。