Google App Engine开发服务器中的自定义HighRepJobPolicy

时间:2012-02-15 16:11:38

标签: google-app-engine

我在App Engine开发服务器中使用自定义HighRepJobPolicy。当我在单元测试中使用它时,同一个类工作正常:

LocalDatastoreServiceTestConfig datastoreConfig = new LocalDatastoreServiceTestConfig();
datastoreConfig.setAlternateHighRepJobPolicyClass(CustomHighRepJobPolicy.class);

但是当我尝试通过添加JVM标记

在Java开发服务器中使用此类时
-Ddatastore.high_replication_job_policy_class=foo.bar.CustomHighRepJobPolicy

我得到一个ClassNotFoundException:

Caused by: java.lang.ClassNotFoundException: foo.bar.CustomHighRepJobPolicy
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at com.google.appengine.tools.development.DevAppServerClassLoader.loadClass(DevAppServerClassLoader.java:87)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:186)
        at com.google.appengine.api.datastore.dev.LocalDatastoreService.initHighRepJobPolicy(LocalDatastoreService.java:426)
        ... 66 more

这是否可以使用,或者之前是否有其他人尝试过此操作?我问,因为我无法通过Google找到任何相关信息,App Engine docs仅提及DefaultHighRepJobPolicy。

1 个答案:

答案 0 :(得分:0)

Looks like you were early to the party

public class LocalCustomPolicyHighRepDatastoreTest {
    private static final class CustomHighRepJobPolicy implements HighRepJobPolicy {
        static int count = 0;
        @Override
        public boolean shouldApplyNewJob(Key entityGroup) {
            // every other new job fails to apply
            return count++ % 2 == 0;
        }

        @Override
        public boolean shouldRollForwardExistingJob(Key entityGroup) {
            // every other existing job fails to apply
            return count++ % 2 == 0;
        }
    }

    private final LocalServiceTestHelper helper =
        new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
           .setAlternateHighRepJobPolicyClass(CustomHighRepJobPolicy.class));

    @Before
    public void setUp() {
        helper.setUp();
    }

    @After
    public void tearDown() {
        helper.tearDown();
    }

    @Test
    public void testEventuallyConsistentGlobalQueryResult() {
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        ds.put(new Entity("yam")); // applies
        ds.put(new Entity("yam")); // does not apply
        // first global query only sees the first Entity
        assertEquals(1, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
        // second global query sees both Entities because we "groom" (attempt to
        // apply unapplied jobs) after every query
        assertEquals(2, ds.prepare(new Query("yam")).countEntities(withLimit(10)));
    }
}