인바운드 큐에서 소비하고 일부 아웃 바운드 대기열 (이 서비스에서 생성 된 다른 스레드가 메시지를 선택하여 대상으로 전송)에 대한 일부 서비스를 제공합니다.Java에서 폴링 차단 사용자를 실행하는 방법은 무엇입니까?
현재 코드에서 볼 수 있듯이 두 개의 일반 Thread
을 사용하지만 일반적으로 더 이상 사용하지 말고 대신 ExecutorService
과 같은 상위 수준의 추상화를 사용해야합니다.
제 경우에 의미가 있습니까? 더 구체적으로 말하자면 ->
- 코드를 줄일 수 있습니까?
- 오류 발생시 코드를보다 강력하게 만드시겠습니까?
- 더 부드러운 스레드 종료가 가능합니까? (테스트를 실행할 때 유용합니다)
여기에 중요한 것이 있습니까?
// called on service startup
private void init() {
// prepare everything here
startInboundWorkerThread();
startOutboundTransporterWorkerThread();
}
private void startInboundWorkerThread() {
InboundWorkerThread runnable = injector.getInstance(InboundWorkerThread.class);
inboundWorkerThread = new Thread(runnable, ownServiceIdentifier);
inboundWorkerThread.start();
}
// this is the Runnable for the InboundWorkerThread
// the runnable for the transporter thread looks almost the same
@Override
public void run() {
while (true) {
InboundMessage message = null;
TransactionStatus transaction = null;
try {
try {
transaction = txManager.getTransaction(new DefaultTransactionDefinition());
} catch (Exception ex) {
// logging
break;
}
// blocking consumer
message = repository.takeOrdered(template, MESSAGE_POLL_TIMEOUT_MILLIS);
if (message != null) {
handleMessage(message);
commitTransaction(message, transaction);
} else {
commitTransaction(transaction);
}
} catch (Exception e) {
// logging
rollback(transaction);
} catch (Throwable e) {
// logging
rollback(transaction);
throw e;
}
if (Thread.interrupted()) {
// logging
break;
}
}
// logging
}
// called when service is shutdown
// both inbound worker thread and transporter worker thread must be terminated
private void interruptAndJoinWorkerThread(final Thread workerThread) {
if (workerThread != null && workerThread.isAlive()) {
workerThread.interrupt();
try {
workerThread.join(TimeUnit.SECONDS.toMillis(1));
} catch (InterruptedException e) {
// logging
}
}
}