1

Hello friends I have a problem

I have a class that extends thread where I want to make a database change using the entity manager, however when trying to use the injected bena it gives me a null

method inside the @service class

private void ejecuteThreadsMap(Map<Integer, List<McDocsEntity>> map) throws Exception {
        List<ChangeNameThread> listThreads = new ArrayList<ChangeNameThread>();
        int num_threads = Integer.valueOf(threads);
        for (int i = 0; i < num_threads; i++) {
            listThreads.add(new ChangeNameThread());
        }

        ArrayList<Integer> list = new ArrayList<Integer>(map.keySet());
        int actual = 0;
        while (actual < list.size()) {
            if (num_threads > list.size())
                num_threads = list.size();
            for (int i = 0; i < num_threads; i++) {
                if (!listThreads.get(i).isAlive()) {
                    System.out.println("actual=" + (actual + 1) + "/" + list.size() + ", porcentaje:" + String.format("%.2f", ((float) ((actual + 1) / (float) list.size()) * 100.0)));
                    listThreads.set(i, new ChangeNameThread());
                    
                    listThreads.get(i).setListaDocs(map.get(list.get(actual)));
                    listThreads.get(i).setId(list.get(actual) + "");
                    listThreads.get(i).start();
                    actual++;
                    if (actual == list.size()) {
                        System.out.println ("**** Se completan " + actual + " elementos");
                        break;
                    }
                }
            }
        
    }

Class ChangeNameThread

@Component
@ComponentScan
public class ChangeNameThread extends Thread {
    
    private String id;
    boolean isCompleted = false;
    private List<McDocsEntity> listaDocs;
    
    
    @Autowired
    private EntityManagerCore coreEntityManager; ----- ERROR NULL
    
        
    ChangeNameThread() {}

    public List<McDocsEntity> getListaDocs() {
        return listaDocs;
    }

    public void setListaDocs(List<McDocsEntity> listaDocs) {
        this.listaDocs = listaDocs;
    }


    public void run() {
        downloadDocs();
        if (isCompleted)
            System.out.println("Termino el hilo["+this.currentThread().getId()+"-"+this.currentThread().getName()+"] para multilateralId:" + multilateralId );
    }

    public boolean getIsCompleted() {
        return isCompleted;
    }

    
    public String getMultilateralId() {
        return multilateralId;
    }

    public void setMultilateralId(String multilateralId) {
        this.multilateralId = multilateralId;
    }

        
        
    public void downloadDocs() {
        
        System.err.println("multilateral id:" + multilateralId);
        try {
            System.out.println("Comienza proceso");
    
            
            for (McDocsEntity mcDocsEntity : listaDocs) {
                changeName(mcDocsEntity);
            }
            
            isCompleted = true;
        } catch (Exception e) {
            System.out.println("Error de gesti\u00f3n de proceso" + System.lineSeparator() + e.getMessage());
        
        }
    }
    
    @Transactional(readOnly = true)
    public void changeName(McDocsEntity mcDocsEntity) {
        
        
        try {
            coreEntityManager.updateDocument(mcDocsEntity.getid,mcDocsEntity.getnewName); //NULLPOINTER
            
        
        } catch (Exception e) {
            System.err.println(e);
        } 
    }
}

I tried to occupy ApplicationContext but I still get the same error

@Autowired
private ApplicationContext applicationContext;

private void ejecuteThreadsMap(Map<Integer, List> map) throws Exception { List listThreads = new ArrayList(); int num_threads = Integer.valueOf(threads); for (int i = 0; i < num_threads; i++) { listThreads.add(new ChangeNameThread()); }

        ArrayList<Integer> list = new ArrayList<Integer>(map.keySet());
        int actual = 0;
        while (actual < list.size()) {
            if (num_threads > list.size())
                num_threads = list.size();
            for (int i = 0; i < num_threads; i++) {
                if (!listThreads.get(i).isAlive()) {
                    System.out.println("actual=" + (actual + 1) + "/" + list.size() + ", porcentaje:" + String.format("%.2f", ((float) ((actual + 1) / (float) list.size()) * 100.0)));
                    listThreads.set(i, new ChangeNameThread());
                    applicationContext.getAutowireCapableBeanFactory().autowireBean(list.get(actual));
                    listThreads.get(i).setListaDocs(map.get(list.get(actual)));
                    listThreads.get(i).setId(list.get(actual) + "");
                    listThreads.get(i).start();
                    actual++;
                    if (actual == list.size()) {
                        System.out.println ("**** Se completan " + actual + " elementos");
                        break;
                    }
                }
            }
        
    }
Sebastian Ruiz
  • 194
  • 2
  • 15

0 Answers0