2014年8月15日星期五

JDG_002:JDG 6.3 Quick Start 例子学习:Hello World

运行环境:JBoss Data Grid 6.3.0

Hello World 只能运行在Library mode下。

学习重点:

1. 如何从Servlet访问Cache ? 
使用CDI依赖注入到Servlet中。
@Inject
DefaultCacheManager m;

2. 如何从JSF页面访问Cache ? 
使用CDI依赖注入到Managed Bean中,然后JSF通过Managed Bean访问Cache。
 @Inject
 DefaultCacheManager m;

3.  如何设置DefaultCacheManager ?

@ApplicationScoped
public class MyCacheManagerProvider {

    private static final long ENTRY_LIFESPAN = 60 * 1000; // 60 seconds

    @Inject
    private Logger log;

    private DefaultCacheManager manager;

    public DefaultCacheManager getCacheManager() {
        if (manager == null) {
            log.info("\n\n DefaultCacheManager does not exist - constructing a new one\n\n");

            GlobalConfiguration glob = new GlobalConfigurationBuilder().clusteredDefault() // Builds a default clustered configuration
                    .transport().addProperty("configurationFile", "jgroups-udp.xml") // provide a specific JGroups configuration
                    .globalJmxStatistics().allowDuplicateDomains(true).enable() // This method enables the jmx statistics of the global configuration and allows for duplicate JMX domains
                    .build(); // Builds the GlobalConfiguration object
            Configuration loc = new ConfigurationBuilder().jmxStatistics().enable() // Enable JMX statistics
                    .clustering().cacheMode(CacheMode.DIST_SYNC) // Set Cache mode to DISTRIBUTED with SYNCHRONOUS replication
                    .hash().numOwners(2) // Keeps two copies of each key/value pair
                    .expiration().lifespan(ENTRY_LIFESPAN) // Set expiration - cache entries expire after some time (given by the lifespan parameter) and are removed from the cache (cluster-wide).
                    .build();
            manager = new DefaultCacheManager(glob, loc, true);
        }
        return manager;
    }

    @PreDestroy
    public void cleanUp() {
        manager.stop();
        manager = null;
    }

}

注意,这里的配置使用了集群方式。

4.  在Servlet和Managed Bean中依赖注入的 DefaultCacheManage 是如何与 MyCacheManagerProvider关联的?

public class Resources {

    @Inject
    MyCacheManagerProvider cacheManagerProvider;

    @Produces
    Logger getLogger(InjectionPoint ip) {
        String category = ip.getMember().getDeclaringClass().getName();
        return Logger.getLogger(category);
    }

    @Produces
    DefaultCacheManager getDefaultCacheManager() {
        return cacheManagerProvider.getCacheManager();
    }

}
这里使用了@Produces,为各种资源“注入”了Logger和DefaultCacheManager对象。

5. 启动JBoss EAP 6.3
(1)启动JBoss EAP Server1
cd /Users/maping/Redhat/Eap/jboss-eap-6.3-jdg-helloworld-1/bin
./standalone.sh
(2)启动JBoss EAP Server 2
cd /Users/maping/Redhat/Eap/jboss-eap-6.3-jdg-helloworld-2/bin
./standalone.sh -Djboss.socket.binding.port-offset=100

6.编译和部署 jboss-helloworld-jdg.war到JBoss EAP Server1和Server2上。


7. 运行
(1)http://localhost:8080/jboss-helloworld-jdg
(2)http://localhost:8180/jboss-helloworld-jdg
无论在哪个Server上操作,另外一个Server上都能“看到”操作后的结果。

没有评论: