1
votes

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'producer' defined in file [C:\Users\guptadee\Projects\Server\Vista\docstash_api_migration\target\classes\com\adp\avs\tax\form\DocStashElasticMigration\Service\Producer.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.concurrent.BlockingQueue' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1201) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1103) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.13.RELEASE.jar:1.5.13.RELEASE] at com.adp.avs.tax.form.DocStashElasticMigration.DocStashElasticMigrationApplication.main(DocStashElasticMigrationApplication.java:29) [classes/:na] Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.concurrent.BlockingQueue' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.17.RELEASE.jar:4.3.17.RELEASE] ... 18 common frames omitted

Migration.java

@Service
public class Migration {

    @Autowired
    private TransportClient transportClient;

    @Autowired
    ClientDao clientDao;

    @Value("#{'${quarterly.form}'.split(',')}")
    private List<String> quarterlyForm;

    @Value("#{'${yearly.form}'.split(',')}")
    private List<String> yearlyForm;

    @Value("${threadCount:5}")
    private int threadCount;


    private final String oldIndex = "taxdocument";

    private final String newTaxformIndex = "";

    private final String newDocumentIndex = "";

    public static String type = "document";


    public void migratetoNewIndex(int year, int qtr) throws Exception{
        //Creating BlockingQueue of size 2000
        BlockingQueue<OutputDocument> queue = new ArrayBlockingQueue<OutputDocument>(2000);
        final Map<String,List<OrganizationUnit>> organizationUnitMap = clientDao.getAllOrg(String.valueOf(year).substring(2), String.valueOf(qtr));
        Producer producer = new Producer(queue,year,qtr);
        Thread producerThread = new Thread(producer);
        producerThread.start();
        List<Thread> consumerList = new ArrayList<Thread>();
        for(int i=0;i<threadCount;i++) {
            Consumer consumer = new Consumer(queue, organizationUnitMap);
            Thread T1 = new Thread(consumer);
            T1.start();
            consumerList.add(T1);
        }
        for (int i = 0; i < consumerList.size(); i++) {
            consumerList.get(i).join();
        }
        System.out.println("Producer and Consumer has been started");

    }

}

Producer.java

@Component
public class Producer implements Runnable {

    @Autowired
    private TransportClient transportClient;

    @Value("#{'${quarterly.form}'.split(',')}")
    private List<String> quarterlyForm;

    @Value("#{'${yearly.form}'.split(',')}")
    private List<String> yearlyForm;

    @Value("${threadCount:5}")
    private int threadCount;

    private final String oldIndex = "taxdocument";

    private final String type = "document";

    private int year;

    private int qtr;


    private BlockingQueue<OutputDocument> queue;

    public Producer(BlockingQueue<OutputDocument> q, int year,int qtr) {
        this.queue = q;
        this.year=year;
        this.qtr=qtr;

    }

    @Override
    public void run() {

        List subcatecory = new ArrayList();
        subcatecory.addAll(quarterlyForm);
        try {
            List<String> yearQtr = new ArrayList<String>();
            yearQtr.add(year + "/" + qtr);
            if (qtr == 4) {
                subcatecory.addAll(yearlyForm);
                yearQtr.add(String.valueOf(year));
            }
            SearchResponse scrollResp = transportClient.prepareSearch(oldIndex)
                    .setTypes(type)
                    .addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
                    .setScroll(new TimeValue(600000))
                    .setSize(1000)
                    .setQuery(boolQuery().must(QueryBuilders.termsQuery("subCategoryCode.codeValue", subcatecory))
                            .must(QueryBuilders.termsQuery("applicationData.yearQuarter", yearQtr)))
                    .get(); //max of 100 hits will be returned for each scroll

            if (scrollResp.getHits().getTotalHits() > 0) {
                OutputDocument outputDocument = null;
                Map<String, Object> responseMap = new HashMap<String, Object>();
                ObjectMapper mapper = new ObjectMapper();
                List<OutputDocument> documentList = new ArrayList<OutputDocument>();
                do {
                    for (SearchHit hit : scrollResp.getHits().getHits()) {
                        responseMap = hit.sourceAsMap();
                        responseMap.remove("storeKey");
                        outputDocument = mapper.convertValue(responseMap, OutputDocument.class);
                        queue.put(outputDocument);
                    }
                    scrollResp = transportClient.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
                } while (scrollResp.getHits().getHits().length != 0); // Zero hits mark the end of the scroll and the while loop.

                //exit message to child threads
                for(int i=0; i<threadCount; i++) {
                    OutputDocument exitMessage = new OutputDocument();
                    exitMessage.setItemID("exit");
                    queue.put(exitMessage);
                }
            }

        } catch (Exception e) {

        }

    }
}
2

2 Answers

2
votes

The message is clear: No bean of type BlockingQueue is found.

You annotated your class as

@Component
public class Producer implements Runnable

Hence Spring will try to initialize a singleton bean of that kind.

As you declared a constructor:

public Producer(BlockingQueue<OutputDocument> q, int year,int qtr) {
    this.queue = q;
    this.year=year;
    this.qtr=qtr;

}

So Spring will try to use that constructor to init the bean.

But first argument BlockingQueue<OutputDocument> q is not found hence the exception.

You need to provide a bean of that type:

@Bean
BlockingQueue<OutputDocument> createBlockingQueue() {
...
}

After this bean is provided, spring will complain about year, qtr too. So you must provide those bean for it. Edit

You have serious problem in your dependency management.

If you declare your Producer as @Component, you should inject it to your Migration class instead of manually create it.

1
votes

See public Producer(BlockingQueue q, int year,int qtr) You dont declared factory instancequalifying bean of type 'java.util.concurrent.BlockingQueue' BlockingQueue You could add factory method or factory bean

@Bean
public BlockingQueue<OutputDocument> createBlockingQueue() {

}