MyTetra Share
Делитесь знаниями!
Создание договоров в несколько потоков
Время создания: 05.12.2019 16:37
Раздел: INFO - JOB - CUBA - GroovyScripts
Запись: wwwlir/Tetra/master/base/1575535078rqrp6tk2dg/text.html на raw.githubusercontent.com

import com.groupstp.rtneo.entity.*;

import com.haulmont.cuba.core.global.*;

import com.groupstp.rtneo.service.*;

import com.haulmont.cuba.core.sys.SecurityContext;

import com.haulmont.cuba.core.sys.AppContext;

import com.haulmont.cuba.core.sys.SecurityContextAwareRunnable

import java.util.concurrent.*;


DataManager dataManager = AppBeans.get(DataManager.NAME);

//ContractService contractService = AppBeans.get(ContractService.NAME);

NotificationService notificationService = AppBeans.get(NotificationService.NAME);


NotificationTemplate notification = dataManager.load(NotificationTemplate.class).id(UUID.fromString('407ed948-072c-7c43-d22a-779ee4653f9f')).view("notificationTemplate-edit").optional().orElse(null)


final SecurityContext securityContext = AppContext.getSecurityContext();


System.out.println("START!!!")

Runnable task = new Runnable() {

public void run() {

AppContext.setSecurityContext(securityContext);

System.out.println("MAIN")

System.out.println(Thread.currentThread().getName())

ExecutorService executorService = Executors.newFixedThreadPool(2)

List<Contragent> contragents = [];

for(def i=0; i<5; i++){

System.out.println("for in "+i)

contragents = dataManager.load(Contragent.class)

.query('select c from rtneo$Contragent c where size(c.realEstates) > 0 and size(c.contracts) <= 1 order by c.id')

.firstResult(i*10)

.maxResults(10)

.view("contragent-create-contract")

.list()

System.out.println("Size contragents" + contragents.size())

System.out.println("START THREAD")

startExec(executorService, contragents)

System.out.println("STOP THREAD FOR " + i)

}

executorService.shutdown()

def done = executorService.awaitTermination(1, TimeUnit.MINUTES);

System.out.println("done "+done)

}


}


Thread thread = new Thread(task);

thread.start();



public void startExec(ExecutorService executorService, List<Contragent> contragents){

System.out.println("Start exec")

Future future = executorService.submit(new SecurityContextAwareRunnable({

try {

System.out.println("GO THREAD!")

System.out.println(Thread.currentThread().getName())

createContract(contragents)

} catch (Exception e) {

log.error('Error creating entity', e)

}

}))

System.out.println("End exec")

}


public void createContract(List<Contragent> contragents){

System.out.println("Start create contragent")

List<ExtUser> users = []

ContractService contractService = AppBeans.get(ContractService.NAME);

for(Contragent contragent : contragents){

System.out.println("check contract")

System.out.println("--"+contragent.getId())

System.out.println(contractService)

contractService.checkContract(contragent, false, true)

System.out.println("check contract OK")

def user = dataManager.load(ExtUser.class)

.query('select u.user from rtneo$Contragent u where u.id = :id')

.parameter("id", contragent.getId())

.view("_local")

.optional()

.orElse(null)

if(user != null)users.add(user)

}

System.out.println("Send notify")

notification.setReceivers(users)

notificationService.send(notification)

System.out.println("Stop create contragent")

}


Так же в этом разделе:
 
MyTetra Share v.0.65
Яндекс индекс цитирования