com.it.j2ee.cache.ehcache.cluster.client2.Client2.java Source code

Java tutorial

Introduction

Here is the source code for com.it.j2ee.cache.ehcache.cluster.client2.Client2.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.it.j2ee.cache.ehcache.cluster.client2;

import java.util.Scanner;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.it.j2ee.spring.SpringContextTestCase;

/**
 * Ehcache?.
 * 
 * ??applicationContext-ehcache.xmlehcache.xml
 * 
 * @author calvin
 */
@ContextConfiguration(locations = { "classpath:com/it/j2ee/modules/cache/ehcache/cluster/c2/spring-ehcache2.xml" })
public class Client2 extends SpringContextTestCase {
    private static final String CACHE_NAME = "colors";

    @Autowired
    private CacheManager ehcacheManager;
    private Cache cache;

    @SuppressWarnings("resource")
    @Test
    public void demo() {
        cache = ehcacheManager.getCache(CACHE_NAME);

        System.out.println("Cluster2: (q?)");

        int i = 0;
        do {
            // cluster1??
            System.out.println("?cluster1-key:");
            String key = new Scanner(System.in).nextLine();
            if ("q".equals(key)) {
                System.exit(0);
            }
            Object result = get(key);
            System.out.println("?cluster1-value:" + result);

            // cluster2cluster1??
            System.out.println("cluster2-key:");
            key = new Scanner(System.in).nextLine();
            System.out.println("cluster2-value:");
            Object value = new Scanner(System.in).nextLine();
            put(key, value);

            i++;
        } while (i < 10);

        // ???
        new Scanner(System.in).nextLine();
    }

    public Object get(String key) {
        Element element = cache.get(key);
        return null == element ? null : element.getObjectValue();
    }

    public void put(String key, Object value) {
        Element element = new Element(key, value);
        cache.put(element);
    }
}