1

I need to execute some code from the Controller in different threads.

My structure:

@Controller MyController //invoked from frontend @RequestMapping MyMethod Loop future = asyncTaskExecutor.Submit(callable) End loop Get future results End MyMethod 

I am using AsyncTaskExecutor like in this example:

Any good Spring threading with a TaskExecutor examples?

public SendEmailController() {} public SendEmailController(AsyncTaskExecutor asyncTaskExecutor) { super(); this.asyncTaskExecutor = asyncTaskExecutor; } 

applicationContext.xml:

<bean id="sendEmailController" class="com.ts.bpu.email.web.SendEmailController"> <constructor-arg ref="asyncTaskExecutor" /> </bean> <bean id="asyncTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="5" /> <property name="maxPoolSize" value="10" /> <property name="queueCapacity" value="25" /> </bean> 

But the constructor with the parameter never is used. But if I use only the constructor with parameter the exception trace is:

 GRAVE: WebModule[/TsSecPricesEngineWeb]StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendEmailController' defined in file [C:\Sun\AppServer\domains\domain1\applications\j2ee-modules\TsSecPricesEngineWeb\WEB-INF\classes\com\ts\bpu\email\web\SendEmailController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ts.bpu.email.web.SendEmailController]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.ts.bpu.email.web.SendEmailController.<init>() at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127) at javax.servlet.GenericServlet.init(GenericServlet.java:270) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1194) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:848) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94) at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096) at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288) at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647) at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579) at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831) at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341) at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263) at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214) at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265) at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106) Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ts.bpu.email.web.SendEmailController]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.ts.bpu.email.web.SendEmailController.<init>() at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:70) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958) ... 44 more Caused by: java.lang.NoSuchMethodException: com.ts.bpu.email.web.SendEmailController.<init>() at java.lang.Class.getConstructor0(Class.java:2706) at java.lang.Class.getDeclaredConstructor(Class.java:1985) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:65) ... 45 more 

Any Suggestion?

Thanks

6
  • If the constructor is never invoked it probably means the bean is never created. Can you declare a default constructor and see if that is being called. If neither are called then there is a problem with your application context in which it isn't being loaded Commented Dec 4, 2012 at 21:44
  • The option that i have tried is: Use two Constructors, One without parameters and other with the asyncTaskExecutor as parameter. With this option, and the applicationContext.xml like i have shown, the second constructor never was used Commented Dec 4, 2012 at 21:56
  • If I use only one contructor with the asyncTaskExecutor as parameter, I get an error in the compilation. Commented Dec 4, 2012 at 21:58
  • In the default constructor set this.asyncTaskExecutor = null and see if that constructor is every being called. Put in a println to ensure its being called Commented Dec 4, 2012 at 21:59
  • John, I have completed my question with the trace exception when i use only the constructor with one parameter Commented Dec 5, 2012 at 7:48

1 Answer 1

0

When Spring container start it will call your SendEmailController class constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I know it, Are there alternative to get an instance of asyncTaskExecutor?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.