kpavlov / jreactive-8583

Kotlin/Java Client & Server for ISO8583 & Netty
Apache License 2.0
321 stars 147 forks source link

Multithread support for Iso853Client? #4

Open keyhan opened 8 years ago

keyhan commented 8 years ago

Hi, just a comment, it would make sense to make some methods like "send()" in client class synchronized. This would make them threadsafe, allowing multiple threads using the same client.

keyhan commented 8 years ago

I have written a thread safe version that is not fully tested but I think could work. I could send it to you if you are interested.

kpavlov commented 8 years ago

Thank you, @Keyhan, send it please. BTW Channel.writeAndFlush is thread-safe so why should we synchronize externally?

keyhan commented 8 years ago

I guess it does not have any timeout handling?

I was thinking something in line with:


private synchronized void send(IsoMessage isoMessage) throws InterruptedException {
        sendAsync(isoMessage).sync().await();
    }

public void orderSend(IsoMessage isoMessage, int timeoutperiod) throws InterruptedException, TimeoutException{
        Runnable orderTask = new Runnable() {
            @Override
            public void run() {
                    try {
                        send(isoMessage);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

            }
        };
        Thread orderThread = new Thread(orderTask);
        orderThread.start();
        Thread.sleep(timeoutperiod);
        if(orderThread.isAlive()) {
            orderThread.interrupt();
            throw new TimeoutException("Thread " + orderThread.getName() + " has timed out.");
        }

    }
VovkaSOL commented 10 months ago

don't use this netty based library with blocking thread timeout handling. For multithread use


   suspend fun suspendSendMsg(request: String): String {
        return suspendCoroutine { continuation ->
            try {
                sendMsg(request) { response ->
                    //continuation.resumeWith(Result.failure(InternalTimeoutException( "Таймаут ответа ${request!!::class.java.canonicalName} со временем ожидания ${finalTimeout.toMillis()} миллисекунд" ) ))
                    continuation.resumeWith(Result.success(response))
                }
            } catch (e: Exception) {
                LOGGER.error("что то сломалось при отправке iso сообщения", e)
                throw RuntimeException("что то сломалось при отправке iso сообщения")
            }
        }
    }
`