Gendaful
Gendaful

Reputation: 5802

How to declare a bean which has a constructor which takes String and Map as parameters?

I am using ThreadPoolExecutor utility and passing the values thru constructor in called class. The constructor takes two arguments (1) a map (2) a string.

I am confused on how to declare a bean for the called class which takes two arguments( a map and a string). My code is as below.

***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;
            }

Thanks Gendaful

Upvotes: 1

Views: 5649

Answers (3)

Gendaful
Gendaful

Reputation: 5802

I have solved this issue as below,

From my calling class, I am passing the instance of Processor class in the execute method.

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

So, what it does is, since I am passing the processor as an argument in the execute(), it will not throw nullpointerexception if I call a method of processor inside run() of ProcessScheduler class.

Upvotes: 0

duffymo
duffymo

Reputation: 308858

Do you really think this is a good idea?

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

I think it should be more like this:

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.
}

I'm not sure that you can inject ProcessScheduler instances from Spring as Beans; this might be a case where you really want to create a new one for each executor service.

Spring need not control every bean in your app.

Upvotes: 2

Santosh Gokak
Santosh Gokak

Reputation: 3411

http://jonathanhui.com/spring-framework-xml-configuration summarizes the various ways of configuring spring beans and supported tags for instantiating and embedding collections.

Something like below should work for you.

<?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>

Upvotes: 0

Related Questions