Connecting multiple flex clients to a single java class

ε祈祈猫儿з 提交于 2019-12-25 07:46:46

问题


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);

        }

回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4335491/connecting-multiple-flex-clients-to-a-single-java-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!