WolfgangB
WolfgangB

Reputation: 21

How to handle global resources in Spring State Machine?

I am thinking of using Spring State Machine for a TCP client. The protocol itself is given and based on proprietary TCP messages with message id and length field. The client sets up a TCP connection to the server, sends a message and always waits for the response before sending the next message. In each state, only certain responses are allowed. Multiple clients must run in parallel.

Now I have the following questions related to Spring State machine.

1) During the initial transition from disconnected to connected the client sets up a connection via java.net.Socket. How can I make this socket (or the DataOutputStream and BufferedReader objects got from the socket) available to the actions of the other transitions?

In this sense, the socket would be some kind of global resource of the state machine. The only way I have seen so far would be to put it in the message headers. But this does not look very natural.

2) Which runtime environment do I need for Spring State Machine?

Is a JVM enough or do I need Tomcat?

Is it thread-safe?

Thanks, Wolfgang

Upvotes: 0

Views: 538

Answers (1)

Janne Valkealahti
Janne Valkealahti

Reputation: 2646

  1. There's nothing wrong using event headers but those are not really global resources as header exists only for duration of a event processing. I'd try to add needed objects into an machine's extended state which is then available for all actions.
  2. You need just JVM. On default machine execution is synchronous so there should not be any threading issues. Docs have notes if you want to replace underlying executor asynchronous(this is usually done if multiple concurrent regions are used).

Upvotes: 0

Related Questions