1

我正在使用 ThreadPoolExecutor 实用程序并通过被调用类中的构造函数传递值。构造函数有两个参数 (1) 一个映射 (2) 一个字符串。

我对如何为带有两个参数(一个映射和一个字符串)的被调用类声明一个 bean 感到困惑。我的代码如下。

***Calling Class***

    public class Starter {

        ProcessScheduler deleteBatch;
        public ProcessScheduler getDeleteBatch() {
            return deleteBatch;
            }

        public void setDeleteBatch(ProcessScheduler deleteBatch) {
            this.deleteBatch = deleteBatch;
            }


        public void start() {

        ThreadPoolExecutor executor = testThreadPoolExecutorService.createNewThreadPool();


            for (int i=0;i<=5;i++)
            {
            Map m4 = arrayRecords.get(i);
            executor.execute(new ProcessScheduler("Thread #"+i,m4));                     // Comment - started 
The above line executes fine but it gives null pointer error if I will call any other method from the run() inside  called class(ProcessScheduler). So I have use a Bean such as executor.execute(getDeleteBatch("Thread #"+i,m4)) to get the instance of the bean. But I dont know how to do this in this case?

// Comment - ended


            }

***Called Class***



public class ProcessScheduler implements Runnable {

         public ProcessScheduler(String taskName, Map m) {
            this.taskName = taskName;
            this.deleteRecordsMap = (HashMap) m;
            }
        Processor processor;
        public Processor getProcessor() 
        {
            return processor;
        }


        public void setProcessor(Processor mappProcessor) {
            this.mappProcessor = mappProcessor;
        }


        public void run() 
        {
        // This returns null 
        processor.getNumbers();
        }

        }



I have some confusions as below.
(1) How to declare a bean for ProcessScheduler in this case. 
(2) Is the declaration of getDeleteBatch is correct in this case like below?

public ProcessScheduler getDeleteBatch() {
            return deleteBatch;
            }

谢谢 Gendaful

4

3 回答 3

2

你真的认为这是个好主意吗?

public ProcessScheduler(String taskName, Map m) {
    this.taskName = taskName;
    this.RecordsMap = (HashMap) m;
}

我认为应该更像这样:

public ProcessScheduler(String taskName, Map m) {
    this.taskName = taskName;
    this.recordsMap = new HashMap(m);  // You don't want changes to the Map passed in to show up in your private data member.
}

我不确定您是否可以将 Spring 中的 ProcessScheduler 实例作为 Bean 注入;这可能是您真的想为每个执行程序服务创建一个新的情况。

Spring 不需要控制应用程序中的每个 bean。

于 2012-11-06T21:00:06.073 回答
0

http://jonathanhui.com/spring-framework-xml-configuration总结了配置 spring bean 的各种方法以及用于实例化和嵌入集合的支持标签。

像下面这样的东西应该适合你。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="processscheduler"
        class="ProcessScheduler">
     <constructor-arg  index="0" value="task1"/>
     <constructor-arg  index="1">
         <map>
              <entry key="key1" value="v1"/>
              <entry key ="key2" value-ref="someBean"/>
          </map>
     </constructor-arg>
  </bean>
</beans>
于 2012-11-06T21:03:32.303 回答
0

我已经解决了这个问题,如下所示,

从我的调用类中,我在执行方法中传递了处理器类的实例。

executor.execute
(newProcessScheduler("Thread#"+Thread.currentThread().getName(),hm,processor))

所以,它的作用是,因为我在 execute() 中将处理器作为参数传递,所以如果我在 ProcessScheduler 类的 run() 中调用处理器的方法,它不会抛出 nullpointerexception。

于 2012-11-15T19:41:52.863 回答