Maybe the best is to make your C code thread safe.
If that is too much work then you can think of a barrier
used to synchronize the C-code-callers at the java level. 
The barrier goes down before the C code is invoked and 
once the legacy-C-code is left, the barrier goes up, and 
allows another thread to enter the same legacy-C-code. 
The third option I can see is as we do with our legacy server 
written in OpenVMS PASCAL, to use / create for each 
incoming thread an java-object talking to his own 
out-of-process server. Can be done by IPC inter-process-channels 
We create for each user-session-thread an out-of-process-server. 
As we have long lasting state full sessions, the creation time has no 
impact to overall session performance.  And in Axis2/J we use 
scope="soapsession" to make a session-thread long-lasting, 
reaching its own "process-placeholder-object". Talking
to unique objects brings you to state-full-service-providing-objects. 
This as opposed to state-less logic. Again, this works only if
you talk to legacy-C-code which is somehow isolated from the
multi thread calling capability of axis2 java sessions. i.e. a
mapping between a thread and a single-threaded-legacy-process
is required.

Josef 

-----Ursprüngliche Nachricht-----
Von: robert lazarski [mailto:robertlazar...@gmail.com] 
Gesendet: Montag, 28. November 2011 21:55
An: java-user@axis.apache.org
Betreff: Re: Calling non-multi-threaded C code in an Axis2 service

On Mon, Nov 28, 2011 at 5:13 PM, Philippe de Rochambeau <phi...@free.fr> wrote:
> Hello,
>
> I would like to create a Web service based on a class which calls C functions 
> via JNI.
>
> The only problem is that the C functions are legacy and therefore not 
> multi-threaded. In other words, if several people simultaneously call the 
> service, they might cause the C code to crash.
>
> What is the best way to make the Axis2 service handle simultaneous calls 
> without crashing or crashing the C code?
>
> Many thanks.
>wa
> p
>

Just invoke a thread in your service. A few lines of code like this
(untested) would work I think, if I understand correctly ... lots of
options also on how to setup queues, etc. One thing that comes to mind
is I do this type of thing with Spring controlled service beans, which
are loaded as singletons IIRC - that I know works. ymmv. Anyways, hope
this helps.

public class MyService {
    private static final ThreadFactory factory = new ThreadFactory();
    private ExecutorService executorService =
Executors.newSingleThreadExecutor(factory);
    private AtomicBoolean isRunning = new AtomicBoolean();

    public OMElement doMyJob(OMElement element) throws XMLStreamException {
        execute();
    }

    public void execute() throws Exception {
        try {
            if (isRunning.compareAndSet(false, true)) {
                // execute asynchronously and return immediately
                executorService.execute(new Runnable() {
                    public void run() {
                        try {
                            // put code here
                        } catch (Exception ex) {
                            logger.error(ex.getMessage(), ex);
                        }
                    }
                });
            }
        } finally {
            isRunning.set(false);
        }
    }

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscr...@axis.apache.org
For additional commands, e-mail: java-user-h...@axis.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscr...@axis.apache.org
For additional commands, e-mail: java-user-h...@axis.apache.org

Reply via email to