0

所以我想我对 memcached 的工作原理有点困惑。我正在使用不同的框架开发相同的 API,所有这些框架都包含 memcached(java 版本 spymemcached)。当我在我的第一个框架上设置 memcached 时,它按预期工作。现在我试图将它合并到我正在使用的第二个框架中,我不断收到一个 ClassNotFoundException ,它试图在我使用的第一个框架中找到一个类,因此不缓存任何东西。也许有人可以启发我了解我的逻辑缺陷。这是两个类(它们是相同的,因为我在不同平台上构建相同的 API),core.Party该类在第一个类中(在运行相应的应用程序时有效),这pojo.PojoParty是我的第一个和第二个框架使用那个不'

这是它发生的类的构造函数:

    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)
4

2 回答 2

1

我想通了,答案很尴尬。所以因为我急于在不同的框架上完成代码,所以我基本上将代码从一个复制粘贴到另一个。从本质上讲,它正在存储第一个 API 中的值。但是,当我尝试使用其他 API 查询这些相同的值时,它试图将这些值存储在 memcache 中的相同键位置。因此它为什么找不到对象,它在错误的项目中!所以我基本上为一个程序附加了字符串键,以便它可以正确返回。

显然这是一个令人尴尬的错误,但我将把它归结为我第一次在多个程序中使用任何类型的缓存系统。

于 2013-08-08T21:21:06.967 回答
0

memcached(或任何序列化程序)尝试从序列化形式重构您的对象时,它必须有权访问适当的类。您在反序列化方面没有core.Party可用的,因此memcached没有用于传入数据流的 Java 表示。

当您谈论“不同的平台”时,您似乎误解了 Java 库的工作原理。由于您在两侧都使用 Java,因此您不应该拥有该类的两个“副本”,您应该只在两者上都有一个可用的类。如果有一些共享代码(例如Party)但有一些其他代码不应该在两个发行版之间共享,则放入Party两个项目都包含的库 jar 中。Maven 和其他构建管理系统使这很容易。

于 2013-08-05T21:32:27.630 回答