[jboss-svn-commits] JBL Code SVN: r14244 - in labs/shotoku/trunk/shotoku-cache: cache-service and 9 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Aug 15 05:46:09 EDT 2007
Author: adamw
Date: 2007-08-15 05:46:09 -0400 (Wed, 15 Aug 2007)
New Revision: 14244
Added:
labs/shotoku/trunk/shotoku-cache/cache-service/src/
labs/shotoku/trunk/shotoku-cache/cache-service/src/etc/
labs/shotoku/trunk/shotoku-cache/cache-service/src/etc/META-INF/
labs/shotoku/trunk/shotoku-cache/cache-service/src/etc/META-INF/jboss-service.xml
labs/shotoku/trunk/shotoku-cache/cache-service/src/java/
labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/
labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/
labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/
labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/cache/
labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/cache/service/
labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/cache/service/RenewableCacheService.java
Removed:
labs/shotoku/trunk/shotoku-cache/cache-base/src/etc/
Log:
Modularazing step 4
Added: labs/shotoku/trunk/shotoku-cache/cache-service/src/etc/META-INF/jboss-service.xml
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-service/src/etc/META-INF/jboss-service.xml (rev 0)
+++ labs/shotoku/trunk/shotoku-cache/cache-service/src/etc/META-INF/jboss-service.xml 2007-08-15 09:46:09 UTC (rev 14244)
@@ -0,0 +1,117 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE server
+ PUBLIC "-//JBoss//DTD MBean Service 4.0//EN"
+ "http://www.jboss.org/j2ee/dtd/jboss-service_4_0.dtd">
+<server>
+ <mbean code="org.jboss.shotoku.cache.service.RenewableCacheService"
+ name="shotoku:service=RenewableCache">
+ <attribute name="Interval">10000</attribute>
+ <attribute name="UpdateThreadCount">3</attribute>
+ <depends optional-attribute-name="TreeCache"
+ proxy-type="attribute">
+ shotoku:service=TreeCache
+ </depends>
+ </mbean>
+
+ <mbean code="org.jboss.cache.TreeCache"
+ name="shotoku:service=TreeCache">
+ <!-- Configure the TransactionManager -->
+ <attribute name="TransactionManagerLookupClass">
+ org.jboss.cache.BatchModeTransactionManagerLookup
+ </attribute>
+
+ <!--
+ Node locking level : SERIALIZABLE
+ REPEATABLE_READ (default)
+ READ_COMMITTED
+ READ_UNCOMMITTED
+ NONE
+ -->
+ <attribute name="IsolationLevel">REPEATABLE_READ</attribute>
+
+ <!--
+ Valid modes are LOCAL
+ REPL_ASYNC
+ REPL_SYNC
+ INVALIDATION_ASYNC
+ INVALIDATION_SYNC
+ -->
+ <attribute name="CacheMode">LOCAL</attribute>
+
+ <!-- Name of cluster. Needs to be the same for all clusters, in order
+ to find each other
+ -->
+ <attribute name="ClusterName">TreeCache-Cluster</attribute>
+
+ <!-- JGroups protocol stack properties. Can also be a URL,
+ e.g. file:/home/bela/default.xml
+ <attribute name="ClusterProperties"></attribute>
+ -->
+
+ <attribute name="ClusterConfig">
+ <config>
+ <!-- UDP: if you have a multihomed machine,
+ set the bind_addr attribute to the appropriate NIC IP address -->
+ <!-- UDP: On Windows machines, because of the media sense feature
+ being broken with multicast (even after disabling media sense)
+ set the loopback attribute to true -->
+ <UDP mcast_addr="228.1.2.3" mcast_port="48866"
+ ip_ttl="64" ip_mcast="true" mcast_send_buf_size="150000"
+ mcast_recv_buf_size="80000" ucast_send_buf_size="150000"
+ ucast_recv_buf_size="80000" loopback="false" />
+ <PING timeout="2000" num_initial_members="3"
+ up_thread="false" down_thread="false" />
+ <MERGE2 min_interval="10000" max_interval="20000" />
+ <FD shun="true" up_thread="true" down_thread="true" />
+ <VERIFY_SUSPECT timeout="1500" up_thread="false"
+ down_thread="false" />
+ <pbcast.NAKACK gc_lag="50"
+ retransmit_timeout="600,1200,2400,4800" max_xmit_size="8192"
+ up_thread="false" down_thread="false" />
+ <UNICAST timeout="600,1200,2400" window_size="100"
+ min_threshold="10" down_thread="false" />
+ <pbcast.STABLE desired_avg_gossip="20000"
+ up_thread="false" down_thread="false" />
+ <FRAG frag_size="8192" down_thread="false"
+ up_thread="false" />
+ <pbcast.GMS join_timeout="5000"
+ join_retry_timeout="2000" shun="true" print_local_addr="true" />
+ <pbcast.STATE_TRANSFER up_thread="false"
+ down_thread="false" />
+ </config>
+ </attribute>
+
+
+ <!--
+ The max amount of time (in milliseconds) we wait until the
+ initial state (ie. the contents of the cache) are retrieved from
+ existing members in a clustered environment
+ -->
+ <attribute name="InitialStateRetrievalTimeout">
+ 20000
+ </attribute>
+
+ <!--
+ Number of milliseconds to wait until all responses for a
+ synchronous call have been received.
+ -->
+ <attribute name="SyncReplTimeout">20000</attribute>
+
+ <!-- Max number of milliseconds to wait for a lock acquisition -->
+ <attribute name="LockAcquisitionTimeout">15000</attribute>
+
+ <!-- Name of the eviction policy class. -->
+ <attribute name="EvictionPolicyClass"></attribute>
+
+ <!--
+ Indicate whether to use region based marshalling or not. Set this to true if you are running under a scoped
+ class loader, e.g., inside an application server. Default is "false".
+ -->
+ <attribute name="UseRegionBasedMarshalling">
+ false
+ </attribute>
+
+ <depends>jboss:service=Naming</depends>
+ <depends>jboss:service=TransactionManager</depends>
+ </mbean>
+</server>
\ No newline at end of file
Added: labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/cache/service/RenewableCacheService.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/cache/service/RenewableCacheService.java (rev 0)
+++ labs/shotoku/trunk/shotoku-cache/cache-service/src/java/org/jboss/shotoku/cache/service/RenewableCacheService.java 2007-08-15 09:46:09 UTC (rev 14244)
@@ -0,0 +1,242 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.shotoku.cache.service;
+
+import org.jboss.cache.CacheException;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.TreeCacheMBean;
+import org.jboss.shotoku.cache.RenewableCacheItem;
+import org.jboss.shotoku.cache.RenewableCacheItemConfiguration;
+import org.jboss.shotoku.cache.SignalExitUpdateThreadData;
+import org.jboss.shotoku.cache.UpdateThread;
+import org.jboss.shotoku.cache.UpdateThreadData;
+import org.jboss.shotoku.tools.ConcurrentSet;
+import org.jboss.shotoku.tools.ConcurrentHashSet;
+import org.jboss.shotoku.tools.CacheTools;
+import org.apache.log4j.Logger;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Set;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ *
+ * @author <a href="mailto:adam.warski at jboss.org">Adam Warski</a>
+ */
+public class RenewableCacheService implements RenewableCacheServiceMBean {
+ private final static Logger log = Logger.getLogger(RenewableCacheServiceMBean.class);
+
+ private TreeCacheMBean treeCache;
+ private long interval;
+ private long lastUpdate;
+ private Thread updateThread;
+
+ /*
+ * Service-handling functions.
+ */
+
+ public void start() {
+ log.info("Starting main update thread.");
+
+ startUpdateThread();
+ }
+
+ public void stop() {
+ getUpdateThread().interrupt();
+
+ log.info("Signaled the main update thread to stop.");
+ }
+
+ /*
+ *
+ */
+
+ public TreeCacheMBean getTreeCache() {
+ return treeCache;
+ }
+
+ public void setTreeCache(TreeCacheMBean treeCache) {
+ this.treeCache = treeCache;
+ }
+
+ public long getInterval() {
+ return interval;
+ }
+
+ public void setInterval(long interval) {
+ this.interval = interval;
+ }
+
+ public long getLastUpdate() {
+ return lastUpdate;
+ }
+
+ public void setLastUpdate(long lastUpdate) {
+ this.lastUpdate = lastUpdate;
+ }
+
+ public long lastUpdateSecondsAgo() {
+ return (System.currentTimeMillis() - getLastUpdate())/1000;
+ }
+
+ public Date lastUpdateDate() {
+ return new Date(getLastUpdate());
+ }
+
+ public Thread getUpdateThread() {
+ return updateThread;
+ }
+
+ public void setUpdateThread(Thread updateThread) {
+ this.updateThread = updateThread;
+ }
+
+ /*
+ *
+ */
+
+ public void startUpdateThread() {
+ Thread ut = new Thread() {
+ {
+ setDaemon(true);
+ }
+
+ public void run() {
+ while (true) {
+ try {
+ sleep(getInterval());
+ } catch (InterruptedException e) {
+ // Quit.
+ log.info("Stopping update thread for " + getName() + " (interrupted).");
+ return;
+ }
+
+ try {
+ update();
+ } catch (Throwable t) {
+ // Making sure that an exception won't stop the thread.
+ log.error("Update method for " + getName() + " threw an exception.", t);
+ }
+
+ setLastUpdate(Calendar.getInstance().getTimeInMillis());
+ }
+ }
+ };
+
+ ut.start();
+ setUpdateThread(ut);
+ }
+
+ /*
+ * Cache handling
+ */
+
+ private final ConcurrentSet<RenewableCacheItem> cacheItems =
+ new ConcurrentHashSet<RenewableCacheItem>();
+
+ public Object get(Fqn fqn, Object key) throws CacheException {
+ return treeCache.get(fqn, key);
+ }
+
+ public void put(Fqn fqn, Object key, Object o) throws CacheException {
+ treeCache.put(fqn, key, o);
+ }
+
+ public void remove(Fqn fqn, Object key) throws CacheException {
+ treeCache.remove(fqn, key);
+ }
+
+ public void register(RenewableCacheItem cacheItem) {
+ cacheItems.add(cacheItem);
+ }
+
+ public void unregister(RenewableCacheItem cacheItem) throws CacheException {
+ cacheItems.remove(cacheItem);
+
+ for (Object key : cacheItem.getKeysUpdates().keySet()) {
+ remove(cacheItem.getFqn(), key);
+ }
+ }
+
+ public Set<? extends RenewableCacheItemConfiguration> getCacheItemsConfigurations() {
+ return cacheItems;
+ }
+
+ private int counter = 0;
+ private final Object counterSync = new Object();
+
+ public Fqn generateNextFqn() {
+ int c;
+ synchronized(counterSync) { c = counter++; }
+
+ return new Fqn(new Object[] {CacheTools.GENERATED_FQN_BASE, c});
+ }
+
+ /*
+ * Update threads management.
+ */
+
+ private final LinkedBlockingQueue<UpdateThreadData> updateThreadDataQueue =
+ new LinkedBlockingQueue<UpdateThreadData>();
+
+ private int updateThreadCount;
+
+ public void addUpdateThreadData(UpdateThreadData data) {
+ updateThreadDataQueue.offer(data);
+ }
+
+ public int getUpdateThreadCount() {
+ return updateThreadCount;
+ }
+
+ public synchronized void setUpdateThreadCount(int n) {
+ if (updateThreadCount < n) {
+ for (int i = updateThreadCount; i < n; i++) {
+ UpdateThread ut = new UpdateThread(this, updateThreadDataQueue);
+ ut.start();
+ }
+ } else if (n < updateThreadCount) {
+ for (int i = updateThreadCount; i > n; i--) {
+ updateThreadDataQueue.offer(new SignalExitUpdateThreadData());
+ }
+ }
+
+ log.info("Update thread count set to: " + n + ".");
+ updateThreadCount = n;
+ }
+
+ /*
+ * Update function.
+ */
+
+ public void update() {
+ for (RenewableCacheItem sci : cacheItems) {
+ try {
+ sci.update();
+ } catch (Throwable t) {
+ log.error("Exception while updating a cache item.", t);
+ }
+ }
+ }
+}
More information about the jboss-svn-commits
mailing list