Author: psharples
Date: Thu Mar 24 12:03:31 2011
New Revision: 1084922

URL: http://svn.apache.org/viewvc?rev=1084922&view=rev
Log:
Queueing framework for sequential db access of preferences and shared data 
updates

Added:
    
incubator/wookie/trunk/src/org/apache/wookie/queues/beans/impl/QueuedBean.java  
 (with props)
    
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueConsumer.java
   (with props)
    
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueHandler.java
   (with props)
    
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueConsumer.java
   (with props)
    
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueHandler.java
   (with props)
    
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueConsumer.java
   (with props)
    
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueHandler.java
   (with props)

Added: 
incubator/wookie/trunk/src/org/apache/wookie/queues/beans/impl/QueuedBean.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/queues/beans/impl/QueuedBean.java?rev=1084922&view=auto
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/queues/beans/impl/QueuedBean.java 
(added)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/queues/beans/impl/QueuedBean.java 
Thu Mar 24 12:03:31 2011
@@ -0,0 +1,76 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.queues.beans.impl;
+
+import org.apache.log4j.Logger;
+import org.apache.wookie.queues.beans.IQueuedBean;
+/**
+ * QueuedBean - model the values needed by the consumer thread to update the 
database
+ * 
+ * @author Paul Sharples
+ *
+ */
+public class QueuedBean implements IQueuedBean {
+       
+       static Logger logger = Logger.getLogger(QueuedBean.class);
+       // instance key
+       protected String id_key;
+       // key to be updated
+       protected String key;
+       // value to update with
+       protected String value;
+       // should it be appended?
+       protected boolean append;
+       
+       public QueuedBean(String idKey, String key, String value, boolean 
append) {
+               super();
+               id_key = idKey;
+               this.key = key;
+               this.value = value;
+               this.append = append;
+       }
+       
+       /*
+        * (non-Javadoc)
+        * @see org.apache.wookie.queues.beans.IQueuedBean#getId_key()
+        */
+       public String getId_key() {
+               return id_key;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.apache.wookie.queues.beans.IQueuedBean#getKey()
+        */
+       public String getKey() {
+               return key;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.apache.wookie.queues.beans.IQueuedBean#getValue()
+        */
+       public String getValue() {
+               return value;
+       }
+       
+       /*
+        * (non-Javadoc)
+        * @see org.apache.wookie.queues.beans.IQueuedBean#append()
+        */
+       public boolean append() {
+               return append;
+       }
+
+}

Propchange: 
incubator/wookie/trunk/src/org/apache/wookie/queues/beans/impl/QueuedBean.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueConsumer.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueConsumer.java?rev=1084922&view=auto
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueConsumer.java
 (added)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueConsumer.java
 Thu Mar 24 12:03:31 2011
@@ -0,0 +1,67 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.queues.impl;
+
+import java.util.concurrent.BlockingQueue;
+
+import org.apache.log4j.Logger;
+import org.apache.wookie.queues.IQueueConsumer;
+import org.apache.wookie.queues.beans.IQueuedBean;
+/**
+ * 
+ * This class is designed to be extended for either a preference scenario or 
shareddata scanario
+ * and contains their common functionality
+ * 
+ * @author Paul Sharples
+ *
+ */
+public abstract class AbstractQueueConsumer extends Thread implements 
IQueueConsumer {
+       
+       public static Logger logger = 
Logger.getLogger(PreferenceQueueConsumer.class);
+       // the queue
+       protected BlockingQueue<IQueuedBean> queue;
+       // the queue identifier
+       protected String queueIdentifer;
+
+       public AbstractQueueConsumer(BlockingQueue<IQueuedBean> queue, String 
queueIdentifer) {
+               super();
+               this.queue = queue;
+               this.queueIdentifer = queueIdentifer;           
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see java.lang.Thread#run()
+        */
+       public void run() {
+               try {
+                       while (true) {
+                               IQueuedBean bean = (IQueuedBean)queue.take();
+                               process(bean);
+                       }                                               
+               } 
+               catch (InterruptedException ex) {               
+                       //logger.info("("+queueIdentifer+")END QueueConsumer 
stopping");
+                       Thread.currentThread().interrupt();                     
+                       return;
+               }
+       }
+       
+       /*
+        * (non-Javadoc)
+        * @see 
org.apache.wookie.queues.IQueueConsumer#process(org.apache.wookie.queues.beans.IQueuedBean)
+        */
+       public abstract void process(IQueuedBean bean);
+
+}

Propchange: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueConsumer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueHandler.java?rev=1084922&view=auto
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueHandler.java
 (added)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueHandler.java
 Thu Mar 24 12:03:31 2011
@@ -0,0 +1,118 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.queues.impl;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import org.apache.wookie.queues.IQueueConsumer;
+import org.apache.wookie.queues.IQueueHandler;
+import org.apache.wookie.queues.beans.IQueuedBean;
+import org.apache.wookie.queues.util.QueueWatcherTask;
+/**
+ * This class mananges the both the queue and the consumer thread which reads 
from the queue
+ * and processes the requests to the database/backend
+ * 
+ * This class is designed to be extended for either a preference scenario or 
shareddata scanario
+ * and contains their common functionality
+ * 
+ * @author Paul Sharples
+ *
+ */
+public abstract class AbstractQueueHandler implements IQueueHandler {  
+       // the queue
+       private BlockingQueue<IQueuedBean> queue = new 
LinkedBlockingQueue<IQueuedBean>();
+       // the queue consumer
+       protected IQueueConsumer queueConsumer;
+       // the thread that watches the queue
+       protected QueueWatcherTask threadWatcher;
+       // the queue identifier
+       protected String queueIdentifer;
+       // a count used to flag when to cancel/stop the queue
+       protected int threadTerminationCount = 1;
+       
+       public AbstractQueueHandler(String queueId) {
+               super();
+               queueIdentifer = queueId;
+               threadWatcher = new QueueWatcherTask(this);     
+       }
+       
+       /**
+        * setup - initialise the queue and start the consumer thread
+        */
+       public void initConsumerQueue(IQueueConsumer qConsumer){
+               queueConsumer = qConsumer;
+               queueConsumer.setName("consumer@"+queueIdentifer);
+               queueConsumer.setDaemon(true);
+               queueConsumer.start();          
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.apache.wookie.queues.IQueueHandler#getQueueIdentifer()
+        */
+       public String getQueueIdentifer() {
+               return queueIdentifer;
+       }
+       
+       /*
+        * (non-Javadoc)
+        * @see 
org.apache.wookie.queues.IQueueHandler#getThreadTerminationCount()
+        */
+       public int getThreadTerminationCount() {
+               return threadTerminationCount;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see 
org.apache.wookie.queues.IQueueHandler#setThreadTerminationCount(int)
+        */
+       public void setThreadTerminationCount(int threadTerminationCount) {
+               this.threadTerminationCount = threadTerminationCount;
+       }
+       
+       /*
+        * (non-Javadoc)
+        * @see org.apache.wookie.queues.IQueueHandler#getQueue()
+        */
+       public BlockingQueue<IQueuedBean> getQueue() {
+               return queue;
+       }
+       
+       /*
+        * 
+        */
+       public void addBeanToQueue(IQueuedBean bean){
+               queue.add(bean);
+               threadTerminationCount++;               
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.apache.wookie.queues.IQueueHandler#destroy()
+        */
+       public void destroy(){
+               queue = null;
+               threadWatcher = null;
+       }
+       
+       /*
+        * (non-Javadoc)
+        * @see org.apache.wookie.queues.IQueueHandler#getConsumer()
+        */
+       public IQueueConsumer getConsumer(){
+               return queueConsumer;
+       }
+
+}

Propchange: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/AbstractQueueHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueConsumer.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueConsumer.java?rev=1084922&view=auto
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueConsumer.java
 (added)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueConsumer.java
 Thu Mar 24 12:03:31 2011
@@ -0,0 +1,64 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.queues.impl;
+
+import java.util.concurrent.BlockingQueue;
+
+import org.apache.log4j.Logger;
+import org.apache.wookie.beans.IWidgetInstance;
+import org.apache.wookie.beans.util.IPersistenceManager;
+import org.apache.wookie.beans.util.PersistenceManagerFactory;
+import org.apache.wookie.controller.PropertiesController;
+import org.apache.wookie.queues.beans.IQueuedBean;
+/**
+ * Implementation of the Preference Queue consumer
+ * 
+ * @author Paul Sharples
+ *
+ */
+public class PreferenceQueueConsumer extends AbstractQueueConsumer {           
+
+       public static Logger logger = 
Logger.getLogger(PreferenceQueueConsumer.class);
+       
+       public PreferenceQueueConsumer(BlockingQueue<IQueuedBean> theQueue, 
String qIdentifier){ 
+               super(theQueue, qIdentifier);
+               this.setName(PreferenceQueueConsumer.class.getName() + ":" + 
qIdentifier);              
+               //logger.info("("+queueIdentifer+")NEW PreferenceQueueConsumer 
starting");
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see 
org.apache.wookie.queues.impl.AbstractQueueConsumer#process(org.apache.wookie.queues.beans.IQueuedBean)
+        */
+   public void process(IQueuedBean bean) {
+       //logger.info("("+queueIdentifer+")CONSUME START 
PreferenceQueueConsumer" + bean.getKey()+ "' TO '" + bean.getValue()+"'");  
+       try {   
+               IPersistenceManager persistenceManager = 
PersistenceManagerFactory.getPersistenceManager();
+               persistenceManager.begin();
+               IWidgetInstance widgetInstance = 
persistenceManager.findWidgetInstanceByIdKey(bean.getId_key());
+               if (widgetInstance != null){
+                       PropertiesController.updatePreference(widgetInstance, 
bean.getKey(), bean.getValue());
+                       persistenceManager.commit();
+               }
+       } 
+       catch (Exception ex) {                  
+               logger.error("("+queueIdentifer+ " to " +bean.getValue() + 
")(Error setting preference: "+ ex, ex);
+       }
+       finally{
+               PersistenceManagerFactory.closePersistenceManager();
+       }
+       //logger.info("("+queueIdentifer+")CONSUME END PreferenceQueueConsumer" 
+ bean.getKey()+ "' TO '" + bean.getValue()+"'");  
+    }
+  }
+

Propchange: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueConsumer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueHandler.java?rev=1084922&view=auto
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueHandler.java
 (added)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueHandler.java
 Thu Mar 24 12:03:31 2011
@@ -0,0 +1,45 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.queues.impl;
+
+import org.apache.log4j.Logger;
+import org.apache.wookie.queues.QueueManager;
+/**
+ * This class handles preference requests to be sent to the database. 
+ * 
+ * @author Paul Sharples
+ *
+ */
+public class PreferenceQueueHandler extends AbstractQueueHandler {
+       
+       public static Logger logger = 
Logger.getLogger(PreferenceQueueHandler.class);
+               
+       public PreferenceQueueHandler(String queueId) {
+               super(queueId);         
+               //logger.info("("+queueIdentifer+" )PreferenceQueueHandler* 
SHOULD ONLY EVER BE CALLED ONCE PER INSTANCE:");            
+               initConsumerQueue(new PreferenceQueueConsumer(getQueue(), 
queueIdentifer));             
+       }
+       
+       /*
+        * (non-Javadoc)
+        * @see org.apache.wookie.queues.impl.AbstractQueueHandler#destroy()
+        */
+       public void destroy(){
+               super.destroy();
+               QueueManager.removePreferenceQueueFromManager(queueIdentifer);  
                                        
+               queueConsumer.interrupt();
+               //logger.info("("+queueIdentifer+" )PreferenceQueueHandler 
DESTROY thread");                            
+       }               
+       
+}

Propchange: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/PreferenceQueueHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueConsumer.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueConsumer.java?rev=1084922&view=auto
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueConsumer.java
 (added)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueConsumer.java
 Thu Mar 24 12:03:31 2011
@@ -0,0 +1,63 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.queues.impl;
+
+import java.util.concurrent.BlockingQueue;
+
+import org.apache.log4j.Logger;
+import org.apache.wookie.beans.IWidgetInstance;
+import org.apache.wookie.beans.util.IPersistenceManager;
+import org.apache.wookie.beans.util.PersistenceManagerFactory;
+import org.apache.wookie.controller.PropertiesController;
+import org.apache.wookie.queues.beans.IQueuedBean;
+/**
+ * Implementation of the shareddata Queue consumer
+ * 
+ * @author Paul Sharples
+ *
+ */
+public class SharedDataQueueConsumer extends AbstractQueueConsumer {           
+
+       public static Logger logger = 
Logger.getLogger(SharedDataQueueConsumer.class);  
+
+       public SharedDataQueueConsumer(BlockingQueue<IQueuedBean> theQueue, 
String qIdentifier){ 
+               super(theQueue, qIdentifier);                            
+               this.setName(SharedDataQueueConsumer.class.getName() + ":" + 
qIdentifier);              
+               //logger.info("("+queueIdentifer+")NEW SharedDataQueueConsumer 
starting");
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see 
org.apache.wookie.queues.impl.AbstractQueueConsumer#process(org.apache.wookie.queues.beans.IQueuedBean)
+        */
+    public void process(IQueuedBean bean) {            
+       //logger.info("("+queueIdentifer+")CONSUME START 
SharedDataQueueConsumer" + bean.getKey()+ "' TO '" + bean.getValue()+"'");  
+       try {   
+               IPersistenceManager persistenceManager = 
PersistenceManagerFactory.getPersistenceManager();
+               persistenceManager.begin();
+               IWidgetInstance widgetInstance = 
persistenceManager.findWidgetInstanceByIdKey(bean.getId_key());
+               if (widgetInstance != null){
+                       
PropertiesController.updateSharedDataEntry(widgetInstance, bean.getKey(), 
bean.getValue(), bean.append());
+                       persistenceManager.commit();
+               }
+       } 
+       catch (Exception ex) {                  
+               logger.error("("+queueIdentifer+ " to " +bean.getValue() + 
")(Error setting SharedData: "+ ex, ex);
+       }
+       finally{
+               PersistenceManagerFactory.closePersistenceManager();
+       }
+       //logger.info("("+queueIdentifer+")CONSUME END SharedDataQueueConsumer" 
+ bean.getKey()+ "' TO '" + bean.getValue()+"'");        
+    }
+  }
\ No newline at end of file

Propchange: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueConsumer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueHandler.java?rev=1084922&view=auto
==============================================================================
--- 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueHandler.java
 (added)
+++ 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueHandler.java
 Thu Mar 24 12:03:31 2011
@@ -0,0 +1,42 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wookie.queues.impl;
+
+import org.apache.log4j.Logger;
+import org.apache.wookie.queues.QueueManager;
+/**
+ * This class handles shared data requests to be sent to the database. 
+ * 
+ * @author Paul Sharples
+ *
+ */
+public class SharedDataQueueHandler extends AbstractQueueHandler {
+       
+       public static Logger logger = 
Logger.getLogger(SharedDataQueueHandler.class);
+                       
+       public SharedDataQueueHandler(String queueKey) {
+               super(queueKey);
+               //logger.info("("+queueIdentifer+" )SharedDataQueueHandler* 
SHOULD ONLY EVER BE CALLED ONCE PER INSTANCE:");            
+               initConsumerQueue(new SharedDataQueueConsumer(getQueue(), 
queueIdentifer));                             
+       }
+               
+       public void destroy(){
+               super.destroy();
+               QueueManager.removeSharedDataQueueFromManager(queueIdentifer);  
                                        
+               queueConsumer.interrupt();                              
+               //logger.info("("+queueIdentifer+" )SharedDataQueueHandler 
DESTROY thread");                            
+       }
+       
+
+}

Propchange: 
incubator/wookie/trunk/src/org/apache/wookie/queues/impl/SharedDataQueueHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to