phax / phase4

phase4 - AS4 client and server for integration into existing systems. Specific support for Peppol and CEF eDelivery built-in.
Apache License 2.0
154 stars 47 forks source link

Using Phase4 in a multi threaded environment #92

Closed chamikaCN closed 2 years ago

chamikaCN commented 2 years ago

Hi Phillip,

I need to run the phase4 project in a multi-threaded environment. I am using the sendMessageAndCheckForReceipt()method to send the AS4 message. But as you have noted, it is non-thread-safe, since the Signal Message Consumer being modified inside the method. What would be the your suggestion to achieve my goal?

PS- I am using phase4 with my own SignalMessageConsumer class, which implements IAS4SignalMessageConsumer, hence the null check aOld == null on line 706 of AbstractAS4UserMessageBuilder.java will always be false.

phax commented 2 years ago

Hi @chamikaCN, Sending multiple AS4 messages in parallel should be no problem, as long as you match the following requirements:

That's all I am aware of

chamikaCN commented 2 years ago

Thanks @phax

chamikaCN commented 2 years ago

Hi @phax,

One thing I noticed regarding this issue is that MockServletContext is also not thread-safe and gets created when a message-sending process is initiated in example tests with WebScopeManager.onGlobalBegin (MockServletContext.create ());. I overcame this problem by ensuring only one context was available when threads handle messages and destroying the context if no thread is in execution. Is there a thread-safe implementation of the ServletContext interface included in this project or any workaround you'd like to suggest?

phax commented 2 years ago

I think the MockServletContext can be made threadsafe quite easily. There is currently no other thread-safe alternative class present - sorry

phax commented 2 years ago

Will be part of ph-web 9.6.5 release

chamikaCN commented 2 years ago

thanks @phax

phax commented 2 years ago

Sorry I needed to tackle some other things so it is part of the ph-web 9.7.0 release and will be part of the phase4 1.4.0 release

phax commented 2 years ago

@chamikaCN Release 1.4.0 from https://github.com/phax/phase4/releases/tag/phase4-parent-pom-1.4.0 should fix the concurrency issue. Please note, that this version switched from Apache HttpClient v4 to v5 which might break existing integrations, so be careful.

chamikaCN commented 2 years ago

Thanks very much for the quick update and the advice as well :slightly_smiling_face: I'll make sure to take precautions prior to upgrading.