Changeset 1314 for raptor-client


Ignore:
Timestamp:
12/19/11 16:16:47 (9 years ago)
Author:
philsmart
Message:
 
Location:
raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store
Files:
1 added
1 deleted
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/DefaultResourceStorageEngine.java

    r1306 r1314  
    2121 
    2222import java.util.List; 
     23import java.util.concurrent.ExecutorService; 
     24import java.util.concurrent.Executors; 
    2325 
    2426import org.slf4j.Logger; 
     
    3234 *  
    3335 */ 
    34 public class ResourceStorageEngine implements StoreEntriesTaskCallbackInterface { 
     36public class DefaultResourceStorageEngine implements ResourceStorageEngine, 
     37        SaveAndApplyResourceClassificationCallbackInterface<StorageResult> { 
    3538 
    3639    /** Class logger */ 
    37     private final Logger log = LoggerFactory.getLogger(ResourceStorageEngine.class); 
     40    private final Logger log = LoggerFactory.getLogger(DefaultResourceStorageEngine.class); 
    3841 
    39     /** Responsible for storing all entries (e.g. events) */ 
     42    /** Responsible for storing all information. */ 
    4043    private RaptorDataConnection dataConnection; 
    4144 
    42     /** The ID of the currently executing transaction */ 
    43     private int currentTransactionId; 
     45    /** The single thread executor service to queue and execute resource storage tasks. */ 
     46    private final ExecutorService resourceClassificationService; 
    4447 
    45     /** Whether a transaction is currently in progress */ 
    46     private boolean transactionInProgress; 
     48    /** Default Constructor. Construct a single threaded executor service <code>resourceClassificationService</code> */ 
     49    public DefaultResourceStorageEngine() { 
     50        resourceClassificationService = Executors.newSingleThreadExecutor(); 
     51    } 
    4752 
    48     /** Default Constructor */ 
    49     public ResourceStorageEngine() { 
     53    public void callback(StorageResult result) { 
     54        log.debug("Storage task completed {}, for transaction id [{}]", result.isSuccess(), result.getTransactionId()); 
    5055 
    5156    } 
    5257 
    53     public void storageResultCallback(Object result) { 
    54         log.debug("Storage task completed {}, for transaction id [{}]", result, currentTransactionId); 
    55         transactionInProgress = false; 
     58    /** {@inheritDoc} */ 
     59    public void performAsynchronousResourceStoragePipeline(List<ResourceMetadata> resources) { 
    5660 
    57     } 
     61        int transactionId = (int) (Math.random() * 1000000); 
    5862 
    59     /** 
    60      * Stores the <code>events</code> asynchronously through the configured 
    61      * {@link uk.ac.cardiff.raptor.store.EventHandler} 
    62      *  
    63      * @param transactionId the numerical Id of this transaction 
    64      * @param events the events to store 
    65      */ 
    66     public void performAsynchronousResourceStoragePipeline(int transactionId, List<ResourceMetadata> resources) 
    67             throws TransactionInProgressException { 
    68         if (transactionInProgress) { 
    69             throw new TransactionInProgressException("Transaction " + currentTransactionId + " currently in processing"); 
    70         } 
    7163        log.info("Committing {} resources to the resource storage engine, with transaction id [{}]", resources.size(), 
    7264                transactionId); 
    73         currentTransactionId = transactionId; 
    74         transactionInProgress = true; 
    7565 
    76         ResourceClassificationBackgroundService backgroundService = 
    77                 new ResourceClassificationBackgroundService(dataConnection); 
    78         backgroundService.saveResourceMetadataAndApplyAsync(resources); 
     66        SaveAndApplyResourceClassificationTask storeEntryTask = 
     67                new SaveAndApplyResourceClassificationTask(transactionId, dataConnection, resources, this); 
     68        resourceClassificationService.submit(storeEntryTask); 
    7969 
    8070    } 
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/ResourceStorageEngine.java

    r1188 r1314  
    1 /** 
    2  * Copyright (C) 2010 Cardiff University, Wales <smartp@cf.ac.uk> 
    3  * 
    4  * Licensed under the Apache License, Version 2.0 (the "License"); 
    5  * you may not use this file except in compliance with the License. 
    6  * You may obtain a copy of the License at 
    7  * 
    8  *         http://www.apache.org/licenses/LICENSE-2.0 
    9  * 
    10  * Unless required by applicable law or agreed to in writing, software 
    11  * distributed under the License is distributed on an "AS IS" BASIS, 
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    13  * See the License for the specific language governing permissions and 
    14  * limitations under the License. 
    15  */ 
    16 /** 
    17  * 
    18  */ 
    191 
    202package uk.ac.cardiff.raptor.store; 
     
    224import java.util.List; 
    235 
    24 import org.slf4j.Logger; 
    25 import org.slf4j.LoggerFactory; 
     6import uk.ac.cardiff.model.resource.ResourceMetadata; 
    267 
    27 import uk.ac.cardiff.model.resource.ResourceMetadata; 
    28 import uk.ac.cardiff.raptor.store.dao.RaptorDataConnection; 
    29  
    30 /** 
    31  * @author philsmart 
    32  *  
    33  */ 
    34 public class ResourceStorageEngine implements StoreEntriesTaskCallbackInterface { 
    35  
    36     /** Class logger */ 
    37     private final Logger log = LoggerFactory.getLogger(ResourceStorageEngine.class); 
    38  
    39     /** Responsible for storing all entries (e.g. events) */ 
    40     private RaptorDataConnection dataConnection; 
    41  
    42     /** The ID of the currently executing transaction */ 
    43     private int currentTransactionId; 
    44  
    45     /** Whether a transaction is currently in progress */ 
    46     private boolean transactionInProgress; 
    47  
    48     /** Default Constructor */ 
    49     public ResourceStorageEngine() { 
    50  
    51     } 
    52  
    53     public void storageResultCallback(Object result) { 
    54         log.debug("Storage task completed {}, for transaction id [{}]", result, currentTransactionId); 
    55         transactionInProgress = false; 
    56  
    57     } 
     8public interface ResourceStorageEngine { 
    589 
    5910    /** 
     
    6415     * @param events the events to store 
    6516     */ 
    66     public void performAsynchronousResourceStoragePipeline(int transactionId, List<ResourceMetadata> resources) 
    67             throws TransactionInProgressException { 
    68         if (transactionInProgress) { 
    69             throw new TransactionInProgressException("Transaction " + currentTransactionId + " currently in processing"); 
    70         } 
    71         log.info("Committing {} resources to the resource storage engine, with transaction id [{}]", resources.size(), 
    72                 transactionId); 
    73         currentTransactionId = transactionId; 
    74         transactionInProgress = true; 
    75  
    76         ResourceClassificationBackgroundService backgroundService = 
    77                 new ResourceClassificationBackgroundService(dataConnection); 
    78         backgroundService.saveResourceMetadataAndApplyAsync(resources); 
    79  
    80     } 
    81  
    82     /** 
    83      * @param dataConnection the dataConnection to set 
    84      */ 
    85     public void setDataConnection(RaptorDataConnection dataConnection) { 
    86         this.dataConnection = dataConnection; 
    87     } 
    88  
    89     /** 
    90      * @return the dataConnection 
    91      */ 
    92     public RaptorDataConnection getDataConnection() { 
    93         return dataConnection; 
    94     } 
     17    public void performAsynchronousResourceStoragePipeline(List<ResourceMetadata> resources); 
    9518 
    9619} 
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/SaveAndApplyResourceClassificationCallbackInterface.java

    r1188 r1314  
    1414 * limitations under the License. 
    1515 */ 
     16 
    1617package uk.ac.cardiff.raptor.store; 
    1718 
    18 public interface SaveAndApplyResourceClassificationCallbackInterface { 
    19          
    20         public void callback(Object result); 
     19public interface SaveAndApplyResourceClassificationCallbackInterface<T> { 
     20 
     21    public void callback(T result); 
    2122 
    2223} 
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/SaveAndApplyResourceClassificationTask.java

    r1188 r1314  
    4343 
    4444    /** The callback interface that is called once the <code>call</code> method has completed */ 
    45     private SaveAndApplyResourceClassificationCallbackInterface storeCallback; 
     45    private SaveAndApplyResourceClassificationCallbackInterface<StorageResult> storeCallback; 
    4646 
    4747    /** The list of resource metadata used to attribute resource classifications to stored events */ 
    48     List<ResourceMetadata> resourceMetadata; 
     48    private List<ResourceMetadata> resourceMetadata; 
    4949 
    50     public SaveAndApplyResourceClassificationTask(RaptorDataConnection dataConnection, 
    51             List<ResourceMetadata> resourceMetadata, SaveAndApplyResourceClassificationCallbackInterface callback) { 
     50    /** Numerical id of this transaction (for tracking). */ 
     51    private int transactionId; 
     52 
     53    public SaveAndApplyResourceClassificationTask(int transactionId, RaptorDataConnection dataConnection, 
     54            List<ResourceMetadata> resourceMetadata, 
     55            SaveAndApplyResourceClassificationCallbackInterface<StorageResult> callback) { 
    5256        this.storeCallback = callback; 
    5357        this.dataConnection = dataConnection; 
    5458        this.resourceMetadata = resourceMetadata; 
     59        this.transactionId = transactionId; 
    5560 
    5661    } 
     
    6570        } catch (DataAccessException e) { 
    6671            log.error("Failed to store events asynchronously", e); 
    67             storeCallback.callback(new Boolean("false")); 
     72            StorageResult result = new StorageResult(); 
     73            result.setSuccess(true); 
     74            result.setTransactionId(transactionId); 
     75            storeCallback.callback(result); 
    6876            return false; 
    6977        } 
    70         storeCallback.callback(new Boolean("true")); 
     78        StorageResult result = new StorageResult(); 
     79        result.setSuccess(false); 
     80        result.setTransactionId(transactionId); 
     81        storeCallback.callback(result); 
    7182        return true; 
    7283    } 
  • raptor-client/trunk/src/main/java/uk/ac/cardiff/raptor/store/StoreEntriesTaskCallbackInterface.java

    r672 r1314  
    1414 * limitations under the License. 
    1515 */ 
     16 
    1617package uk.ac.cardiff.raptor.store; 
    1718 
    1819public interface StoreEntriesTaskCallbackInterface { 
    19          
    20         public void storageResultCallback(Object result); 
     20 
     21    public void storageResultCallback(Object result); 
    2122 
    2223} 
Note: See TracChangeset for help on using the changeset viewer.