You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ni...@apache.org on 2006/09/26 20:23:00 UTC
svn commit: r450138 - in /directory/trunks/mina/integration-spring/src:
main/java/org/apache/mina/integration/spring/
test/java/org/apache/mina/integration/spring/
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 <code>serviceName</code>
+ * 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
+ * <code>null</code>.
+ */
+ 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
+ * <code>null</code>.
+ */
+ 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 <code>ThreadPoolTaskExecutor</code> 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
+ * <code>Integer.MAX_VALUE</code>.
+ */
+ 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
+ * <code>Integer.MAX_VALUE</code>.
+ * <p>
+ * 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