Connecting multiple flex clients to a single java class

I have a multi-user application consisting of a flex client and blazeds/Spring/java backend - I have the main elements working fine ie. sending messages to destination, consuming and producing. Flex clients are able to send and retrieve a string from this class no problem. What I want to do is to have the 2 clients with access to the same variable..in this crude sample I'm sending a guid from each swf which I append to a string _players server side. What happens is when I launch Swf A, it recieves its guid back fine, as does Swf B. Then Swf A recieves the guid from Swf B, but Swf B does not recieve Swf A. BTW this is the same swf code just launched twice each in a different browser.

Can anyone see where I'm going wrong or what might be a better solution?

public class GameFeed {

    private static GaneFeedThread thread;

    private final MessageTemplate template;

    public GameFeed(MessageTemplate template) {
        this.template = template;
    }

    public void start() {
        if (thread == null) {
            thread = new GaneFeedThread(this.template);
            thread.start();
        }
    }

    public void stop() {
        thread.running = false;
        thread = null;
    }

    public static class GaneFeedThread extends Thread {

        public boolean running = false;

        private final MessageTemplate template;

        public GaneFeedThread(MessageTemplate template) {
            this.template = template;
        }

        private static String _players;

        public void addPlayer(String name)
        {
            _players += name + ",";
        }
        while (this.running) {


                this.template.send("game-feed", _players);

        }

Answers


You have a threading problem in you class. It is not sure if this is the cause of your problem - but it could.

It seams that you are sharing data though the _player variable. But this variable is not thread safe. It has two major problem:

  • issue 1 : If two clients invoke the addPlayer method at the same time - it is not clear what happen to your player variable - alt least you could have something like a lost update
  • issue 2: (this is maybe the cause) - The Java memory model does not guarantee that the _player variable is updated in both threads without proper concurrency management.

To fix it you have to do two things:

  • first: wrap _players += name + ","; in an synchronized block (for issue 1)
  • second: mark _players as volatile (for issue 2)

@see http://jeremymanson.blogspot.com/2008/11/what-volatile-means-in-java.html


It's probably the server that's preventing this. Traditionally, data that is to be shared between clients, or otherwise persisted, is written to a DB or some other datasource. You might do well with a in memory DB. Most web servers have one configured out of the box using HSQLDB or Derby.


A general other solution would be using a thread save collection instead of the String, but this my lead to other problems and is not so efficent like your string.

But nevertheless you should rething your decision: to use a static variable in a Thread class to store shared business data like your player list.


Need Your Help

Change associated textbox color with jquery on client validation

jquery asp.net validation

I want to use jquery to select an associated element's parent that is required and change the css class.

Batch - setting a variable via IF statement in FOR loop “stuck”

variables batch-file if-statement for-loop

I'm working on this lil' script that reads columns from the input file:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.