Skip to content
Permalink
Browse files
Moving spring-taskqueue component to archiva-components repository
  • Loading branch information
effrafax committed Nov 21, 2019
1 parent a5e23fa commit 5a26d3c39d1877a207eca5726bda522cee317899
Showing 25 changed files with 1,960 additions and 0 deletions.
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.archiva.components</groupId>
<artifactId>archiva-components</artifactId>
<version>3.0-SNAPSHOT</version>
</parent>

<version>3.0-SNAPSHOT</version>
<artifactId>archiva-components-spring-taskqueue</artifactId>

<name>Archiva Components :: Spring Task Queue</name>

<properties>
<site.staging.base>${project.parent.basedir}</site.staging.base>
</properties>

<url>${webUrl}/${project.artifactId}</url>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
@@ -0,0 +1,217 @@
package org.apache.archiva.components.taskqueue;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;


/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
*
*/
public class DefaultTaskQueue
implements TaskQueue
{

private Logger logger = LoggerFactory.getLogger( getClass() );

private List<TaskEntryEvaluator> taskEntryEvaluators = new ArrayList<>();

private List<TaskExitEvaluator> taskExitEvaluators = new ArrayList<>();

private List<TaskViabilityEvaluator> taskViabilityEvaluators = new ArrayList<>();

private BlockingQueue<Task> queue = new LinkedBlockingQueue<>();

// ----------------------------------------------------------------------
// Component Lifecycle
// ----------------------------------------------------------------------

// ----------------------------------------------------------------------
// TaskQueue Implementation
// ----------------------------------------------------------------------

// ----------------------------------------------------------------------
// Queue operations
// ----------------------------------------------------------------------

public boolean put( Task task )
throws TaskQueueException
{
// ----------------------------------------------------------------------
// Check that all the task entry evaluators accepts the task
// ----------------------------------------------------------------------

for ( TaskEntryEvaluator taskEntryEvaluator : taskEntryEvaluators )
{
boolean result = taskEntryEvaluator.evaluate( task );

if ( !result )
{
return false;
}
}

// ----------------------------------------------------------------------
// The task was accepted, enqueue it
// ----------------------------------------------------------------------

enqueue( task );

// ----------------------------------------------------------------------
// Check that all the task viability evaluators accepts the task
// ----------------------------------------------------------------------

for ( TaskViabilityEvaluator taskViabilityEvaluator : taskViabilityEvaluators )
{
Collection<Task> toBeRemoved =
taskViabilityEvaluator.evaluate( Collections.unmodifiableCollection( queue ) );

for ( Iterator<Task> it = toBeRemoved.iterator(); it.hasNext(); )
{
Task t = it.next();

queue.remove( t );
}
}

return true;
}

public Task take()
throws TaskQueueException
{
logger.debug( "take" );
while ( true )
{
Task task = dequeue();

if ( task == null )
{
return null;
}

for ( TaskExitEvaluator taskExitEvaluator : taskExitEvaluators )
{
boolean result = taskExitEvaluator.evaluate( task );

if ( !result )
{
// the task wasn't accepted; drop it.
task = null;

break;
}
}

if ( task != null )
{
return task;
}
}
}

public Task poll( int timeout, TimeUnit timeUnit )
throws InterruptedException
{
logger.debug( "pool" );
return queue.poll( timeout, timeUnit );
}

public boolean remove( Task task )
throws ClassCastException, NullPointerException
{
return queue.remove( task );
}

public boolean removeAll( List tasks )
throws ClassCastException, NullPointerException
{
return queue.removeAll( tasks );
}


// ----------------------------------------------------------------------
// Queue Inspection
// ----------------------------------------------------------------------

public List<Task> getQueueSnapshot()
throws TaskQueueException
{
return Collections.unmodifiableList( new ArrayList( queue ) );
}

// ----------------------------------------------------------------------
// Queue Management
// ----------------------------------------------------------------------

private void enqueue( Task task )
{
boolean success = queue.add( task );
logger.debug( "enqueue success {}", success );
}

private Task dequeue()
{
logger.debug( "dequeue" );
return queue.poll();
}

public List<TaskEntryEvaluator> getTaskEntryEvaluators()
{
return taskEntryEvaluators;
}

public void setTaskEntryEvaluators( List<TaskEntryEvaluator> taskEntryEvaluators )
{
this.taskEntryEvaluators = taskEntryEvaluators;
}

public List<TaskExitEvaluator> getTaskExitEvaluators()
{
return taskExitEvaluators;
}

public void setTaskExitEvaluators( List<TaskExitEvaluator> taskExitEvaluators )
{
this.taskExitEvaluators = taskExitEvaluators;
}

public List<TaskViabilityEvaluator> getTaskViabilityEvaluators()
{
return taskViabilityEvaluators;
}

public void setTaskViabilityEvaluators( List<TaskViabilityEvaluator> taskViabilityEvaluators )
{
this.taskViabilityEvaluators = taskViabilityEvaluators;
}
}
@@ -0,0 +1,33 @@
package org.apache.archiva.components.taskqueue;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
*
*/
public interface Task
{
/**
* @return the maximum time in milliseconds this task may run before it's cancelled.
*/
long getMaxExecutionTime();
}
@@ -0,0 +1,31 @@
package org.apache.archiva.components.taskqueue;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

/**
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
*
*/
public interface TaskEntryEvaluator<T extends Task>
{
boolean evaluate( T task )
throws TaskQueueException;
}

0 comments on commit 5a26d3c

Please sign in to comment.