Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 3723 invoked from network); 26 Sep 2006 18:23:04 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 26 Sep 2006 18:23:04 -0000 Received: (qmail 2076 invoked by uid 500); 26 Sep 2006 18:23:04 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 2024 invoked by uid 500); 26 Sep 2006 18:23:04 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 2013 invoked by uid 99); 26 Sep 2006 18:23:04 -0000 Received: from idunn.apache.osuosl.org (HELO idunn.apache.osuosl.org) (140.211.166.84) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Sep 2006 11:23:04 -0700 Authentication-Results: idunn.apache.osuosl.org smtp.mail=niklas@apache.org; spf=permerror X-ASF-Spam-Status: No, hits=-9.4 required=5.0 tests=ALL_TRUSTED,NO_REAL_NAME Received-SPF: error (idunn.apache.osuosl.org: domain apache.org from 140.211.166.113 cause and error) Received: from [140.211.166.113] ([140.211.166.113:54372] helo=eris.apache.org) by idunn.apache.osuosl.org (ecelerity 2.1.1.8 r(12930)) with ESMTP id EC/51-14537-70079154 for ; Tue, 26 Sep 2006 11:23:03 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id AD32E1A981A; Tue, 26 Sep 2006 11:23:00 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r450138 - in /directory/trunks/mina/integration-spring/src: main/java/org/apache/mina/integration/spring/ test/java/org/apache/mina/integration/spring/ Date: Tue, 26 Sep 2006 18:23:00 -0000 To: commits@directory.apache.org From: niklas@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20060926182300.AD32E1A981A@eris.apache.org> X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: niklas Date: Tue Sep 26 11:22:59 2006 New Revision: 450138 URL: http://svn.apache.org/viewvc?view=rev&rev=450138 Log: Added ExecutorThreadModelFactoryBean and ThreadPoolExecutorFactoryBean which make it simple to setup an ExecutorThreadModel using Spring. Updated the Javadoc of IoAcceptorFactoryBean. Added: directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBean.java (with props) directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ThreadPoolExecutorFactoryBean.java (with props) directory/trunks/mina/integration-spring/src/test/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBeanTest.java (with props) Modified: directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/IoAcceptorFactoryBean.java Added: directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBean.java URL: http://svn.apache.org/viewvc/directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBean.java?view=auto&rev=450138 ============================================================================== --- directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBean.java (added) +++ directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBean.java Tue Sep 26 11:22:59 2006 @@ -0,0 +1,97 @@ +/* + * 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. + * + */ +package org.apache.mina.integration.spring; + +import org.apache.mina.common.ExecutorThreadModel; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.Assert; + +import edu.emory.mathcs.backport.java.util.concurrent.Executor; + +/** + * Spring {@link FactoryBean} which makes it possible to set up a MINA + * {@link ExecutorThreadModel} using Spring. The serviceName + * property must be set using {@link #setServiceName(String)}. + * + * @author The Apache Directory Project (mina-dev@directory.apache.org) + * @version $Rev$, $Date$ + */ +public class ExecutorThreadModelFactoryBean implements FactoryBean, InitializingBean +{ + private String serviceName = null; + private Executor executor = null; + + /** + * Sets the {@link Executor} to use. If not set a default {@link Executor} + * will be used by the {@link ExecutorThreadModel} created by this + * factory bean. + * + * @param executor the executor. + * @throws IllegalArgumentException if the specified value is + * null. + */ + public void setExecutor( Executor executor ) + { + Assert.notNull( executor, "Property 'executor' may not be null" ); + this.executor = executor; + } + + /** + * Sets the name of the service as used in the call to + * {@link ExecutorThreadModel#getInstance(String)}. This property is + * required. + * + * @param executor the executor. + * @throws IllegalArgumentException if the specified value is + * null. + */ + public void setServiceName( String serviceName ) + { + Assert.notNull( serviceName, "Property 'serviceName' may not be null" ); + this.serviceName = serviceName; + } + + public Class getObjectType() + { + return ExecutorThreadModel.class; + } + + public Object getObject() throws Exception + { + ExecutorThreadModel model = ExecutorThreadModel.getInstance( serviceName ); + if( executor != null ) + { + model.setExecutor( executor ); + } + return model; + } + + public boolean isSingleton() + { + return true; + } + + public void afterPropertiesSet() throws Exception + { + Assert.notNull( serviceName, "Property 'serviceName' may not be null" ); + } + +} Propchange: directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBean.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/IoAcceptorFactoryBean.java URL: http://svn.apache.org/viewvc/directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/IoAcceptorFactoryBean.java?view=diff&rev=450138&r1=450137&r2=450138 ============================================================================== --- directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/IoAcceptorFactoryBean.java (original) +++ directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/IoAcceptorFactoryBean.java Tue Sep 26 11:22:59 2006 @@ -63,18 +63,21 @@ * </property> * </bean> * - * <!-- By default MINA uses a PooledThreadModel. This demonstrates how to + * <!-- By default MINA uses an ExecutorThreadModel. This demonstrates how to * use your own with some non default settings. The threadModel will * be set on the SocketAcceptorConfig defined below. To configure a - * ThreadPoolFilter directly you will have to use the ThreadModel.MANUAL + * ExecutorFilter directly you will have to use the ThreadModel.MANUAL * ThreadModel instead. --> - * <property name="threadModel"> - * <bean class="org.apache.mina.common.PooledThreadModel"> - * <!-- Threads will be named IoWorker-1, IoWorker-2, etc --> - * <property name="threadNamePrefix" value="IoWorker"/> - * <property name="maximumPoolSize" value="10"/> - * <property name="keepAliveTime" value="30000"/> - * </bean> + * <bean id="threadModel" class="org.apache.mina.integration.spring.ExecutorThreadModelFactoryBean"> + * <property name="serviceName" value="HttpService"/> + * <property name="executor"> + * <bean class="org.apache.mina.integration.spring.ThreadPoolExecutorFactoryBean"> + * <property name="corePoolSize" value="2"/> + * <property name="maxPoolSize" value="10"/> + * <property name="keepAliveSeconds" value="30"/> + * </bean> + * </property> + * </bean> * * <bean id="ioAcceptor" class="org.apache.mina.integration.spring.IoAcceptorFactoryBean"> * <property name="target"> Added: directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ThreadPoolExecutorFactoryBean.java URL: http://svn.apache.org/viewvc/directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ThreadPoolExecutorFactoryBean.java?view=auto&rev=450138 ============================================================================== --- directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ThreadPoolExecutorFactoryBean.java (added) +++ directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ThreadPoolExecutorFactoryBean.java Tue Sep 26 11:22:59 2006 @@ -0,0 +1,150 @@ +/* + * 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. + * + */ +package org.apache.mina.integration.spring; + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.config.AbstractFactoryBean; + +import edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue; +import edu.emory.mathcs.backport.java.util.concurrent.Executors; +import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingQueue; +import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler; +import edu.emory.mathcs.backport.java.util.concurrent.SynchronousQueue; +import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory; +import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor; +import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; + +/** + * Spring {@link FactoryBean} which enables the configuration of + * {@link ThreadPoolExecutor} instances using Spring. Most of this code + * has been copied from the ThreadPoolTaskExecutor class + * available in Spring 2.0. + * + * @author The Apache Directory Project (mina-dev@directory.apache.org) + * @version $Rev$, $Date$ + */ +public class ThreadPoolExecutorFactoryBean extends AbstractFactoryBean +{ + private int corePoolSize = 1; + + private int maxPoolSize = Integer.MAX_VALUE; + + private int keepAliveSeconds = 60; + + private int queueCapacity = Integer.MAX_VALUE; + + private ThreadFactory threadFactory = Executors.defaultThreadFactory(); + + private RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.AbortPolicy(); + + /** + * Set the ThreadPoolExecutor's core pool size. Default is 1. + */ + public void setCorePoolSize( int corePoolSize ) + { + this.corePoolSize = corePoolSize; + } + + /** + * Set the ThreadPoolExecutor's maximum pool size. Default is + * Integer.MAX_VALUE. + */ + public void setMaxPoolSize( int maxPoolSize ) + { + this.maxPoolSize = maxPoolSize; + } + + /** + * Set the ThreadPoolExecutor's keep alive seconds. Default is 60. + */ + public void setKeepAliveSeconds( int keepAliveSeconds ) + { + this.keepAliveSeconds = keepAliveSeconds; + } + + /** + * Set the capacity for the ThreadPoolExecutor's BlockingQueue. Default is + * Integer.MAX_VALUE. + *

+ * Any positive value will lead to a LinkedBlockingQueue instance; any other + * value will lead to a SynchronousQueue instance. + * + * @see LinkedBlockingQueue + * @see SynchronousQueue + */ + public void setQueueCapacity( int queueCapacity ) + { + this.queueCapacity = queueCapacity; + } + + /** + * Set the ThreadFactory to use for the ThreadPoolExecutor's thread pool. + * Default is the ThreadPoolExecutor's default thread factory. + * + * @see Executors#defaultThreadFactory() + */ + public void setThreadFactory( ThreadFactory threadFactory ) + { + this.threadFactory = ( threadFactory != null ? threadFactory + : Executors.defaultThreadFactory() ); + } + + /** + * Set the RejectedExecutionHandler to use for the ThreadPoolExecutor. + * Default is the ThreadPoolExecutor's default abort policy. + * + * @see ThreadPoolExecutor.AbortPolicy + */ + public void setRejectedExecutionHandler( + RejectedExecutionHandler rejectedExecutionHandler ) + { + this.rejectedExecutionHandler = ( rejectedExecutionHandler != null + ? rejectedExecutionHandler + : new ThreadPoolExecutor.AbortPolicy() ); + } + + protected Object createInstance() throws Exception + { + BlockingQueue queue = null; + if( queueCapacity > 0 ) + { + queue = new LinkedBlockingQueue( queueCapacity ); + } + else + { + queue = new SynchronousQueue(); + } + return new ThreadPoolExecutor( + corePoolSize, maxPoolSize, keepAliveSeconds, TimeUnit.SECONDS, + queue, threadFactory, rejectedExecutionHandler ); + } + + protected void destroyInstance( Object o ) throws Exception + { + ThreadPoolExecutor executor = ( ThreadPoolExecutor ) o; + executor.shutdown(); + } + + public Class getObjectType() + { + return ThreadPoolExecutor.class; + } + +} Propchange: directory/trunks/mina/integration-spring/src/main/java/org/apache/mina/integration/spring/ThreadPoolExecutorFactoryBean.java ------------------------------------------------------------------------------ svn:keywords = Id Added: directory/trunks/mina/integration-spring/src/test/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBeanTest.java URL: http://svn.apache.org/viewvc/directory/trunks/mina/integration-spring/src/test/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBeanTest.java?view=auto&rev=450138 ============================================================================== --- directory/trunks/mina/integration-spring/src/test/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBeanTest.java (added) +++ directory/trunks/mina/integration-spring/src/test/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBeanTest.java Tue Sep 26 11:22:59 2006 @@ -0,0 +1,71 @@ +/* + * 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. + * + */ +package org.apache.mina.integration.spring; + +import junit.framework.TestCase; + +import org.apache.mina.common.ExecutorThreadModel; + +import edu.emory.mathcs.backport.java.util.concurrent.Executor; +import edu.emory.mathcs.backport.java.util.concurrent.SynchronousQueue; +import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor; +import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; + +/** + * Tests {@link ExecutorThreadModelFactoryBean}. + * + * @author The Apache Directory Project (mina-dev@directory.apache.org) + * @version $Rev$, $Date$ + */ +public class ExecutorThreadModelFactoryBeanTest extends TestCase +{ + public void testSuccessfulCreationWithExecutor() throws Exception + { + Executor executor = new ThreadPoolExecutor( 1, 10, 3600, TimeUnit.SECONDS, new SynchronousQueue() ); + ExecutorThreadModelFactoryBean factory = new ExecutorThreadModelFactoryBean(); + factory.setServiceName( "foo" ); + factory.setExecutor( executor ); + factory.afterPropertiesSet(); + ExecutorThreadModel threadModel = ( ExecutorThreadModel ) factory.getObject(); + assertSame( executor, threadModel.getExecutor() ); + } + + public void testSuccessfulCreationWithoutExecutor() throws Exception + { + ExecutorThreadModelFactoryBean factory = new ExecutorThreadModelFactoryBean(); + factory.setServiceName( "foo" ); + factory.afterPropertiesSet(); + ExecutorThreadModel threadModel = ( ExecutorThreadModel ) factory.getObject(); + assertTrue( threadModel.getExecutor() instanceof ThreadPoolExecutor ); + } + + public void testUnsuccessfulCreation() throws Exception + { + ExecutorThreadModelFactoryBean factory = new ExecutorThreadModelFactoryBean(); + try + { + factory.afterPropertiesSet(); + fail( "No serviceName set. IllegalArgumentException expected." ); + } + catch( IllegalArgumentException iae ) + { + } + } +} Propchange: directory/trunks/mina/integration-spring/src/test/java/org/apache/mina/integration/spring/ExecutorThreadModelFactoryBeanTest.java ------------------------------------------------------------------------------ svn:keywords = Id