Hello.

As a learning exercise, I've put together a very contrived example of a
"reverse" service (like an echo service, except that each returned line
comes back reversed). Mostly, I'm using it as a learning example of how
to build a small TCP/IP client/server in OSGi.

There are five bundles:

  reverser-api    - Specifies the API of the reverse service
  reverser-simple - A mindlessly simple implementation of the API
  tcp-server      - A server that exports nothing and implements
                    no services, but simply passes on the input
                    from clients to a local reverser-api implementation.
  tcp-client      - A client that implements the reverser-api service
                    and relays calls to a remote server, using prototype
                    scope so that each component gets its own client.
  client-example  - A simple bundle that makes calls to a reverser-api
                    service. Mostly used to play with tcp-client.

What I have works, for a given value of works, but has a problem in
that it has a race condition when restarting the server service:

  
https://github.com/io7m/osgi-example-reverser/blob/master/tcp-server/src/main/java/com/io7m/reverser/tcp_server/TCPServerService.java

In activate(), the service creates a new TCPServer on a new thread
and starts it. In deactivate(), the service tells the existing
TCPServer to stop. The stop call happens asynchronously, because all it
does is set a flag on the TCPServer that will cause it to eventually
shut down (typically within one second). The problem will probably be
obvious to many people here: When the service is restarted, the old
server may not have finished shutting down, and therefore when the new
instance tries to bind a socket, it receives a java.net.BindException
because the address is still in use. Worse, because the new server
fails to start and the old one is in the process of shutting down, the
end result is no server running at all.

Is there a correct way to do this? I inferred from what I've read in
various places online that I should not be blocking in the activate()
or deactivate() methods as this can disrupt the SCR.

M

Attachment: pgpc3pWXOOwQd.pgp
Description: OpenPGP digital signature

_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to