Java осень 2012 лекция 3

  • View
    43

  • Download
    1

Embed Size (px)

Transcript

Java 3. Message System

Message SystemFrontend Account ServiceConcurrent Collections

userId hidden input jetty localhost:8080Jetty userId ( ) (post) userId

( ) browserserverstart on localhost:8080http://localhost:8080/generate userIdfirst loadform with userIdautopostHello user with: userIdform with userId

: Post + sessionId 2: + + userId + sessionId + sessionId

Id handle() Accounter userId Accounter, Accounter

browserserverstart on localhost:8080http://localhost:8080/generate sessionIdfirst accessenter your namesubmit nameload userIdHello user!

Thread-pool Jetty

Message SystemFrontend Account ServiceConcurrent Collections

Frontend Account ServiceFrontend Accounter Frontend Account Service Frontend c Frontend Account Service , Frontend

Frontend Account Servicefrontendaccount serviceget Id from Storageget Id by namereturn Id for nameHello user!submit namewait for authcheck statewait for authcheck state

Account Account Jetty

?

:: Account Service userSession Account Service userData wait() notify() Frontend Account Service

Message SystemFrontend Account ServiceConcurrent Collections

Concurrent CollectionsArrayBlockingQueue ConcurrentMapCopyOnWriteArrayListCopyOnWriteArraySetLinkedBlockingQueue

Message SystemFrontend Account ServiceConcurrent Collections

:Thread-Safe : Thread-local :

Message SystemMessageSystem

Address Abonentpublic class Address {static private AtomicInteger abonentIdCreator = new AtomicInteger();final private int abonentId;

public Address(){this.abonentId = abonentIdCreator.incrementAndGet();}

public int getAbonentId() {return abonentId;}}public interface Abonent {Address getAddress();}

Messagepublic abstract class Msg {final private Address from;final private Address to;

public Msg(Address from, Address to){this.from = from;this.to = to;}

protected Address getFrom(){return from;}

protected Address getTo(){return to;}

public abstract void exec(Abonent abonent);}

Message to Frontendpublic abstract class MsgToFrontend extends Msg {

public MsgToFrontend(Address from, Address to) {super(from, to);}

public void exec(Abonent abonent) {if(abonent instanceof Frontend){exec((Frontend)abonent);}}

public abstract void exec(Frontend frontend);}

Message to Frontendpublic class MsgUpdateUserId extends MsgToFrontend {

final private int sessionId;final private int userId;

public GameInfoMsg(Address from, Address to, int sessionId, int userId) {super(from, to);this. sessionId = sessionId;this. userId = userId;}

public void exec(Frontend frontend) {frontend.updateUserId(sessionId, userId);}}

Message Systemprivate Map messages = new HashMap();

public void sendMessage(Msg message){Queue messageQueue = messages.get(message.getTo());messageQueue.add(message);}

public void execForAbonent(Abonent abonent) {Queue messageQueue = messages.get(abonent.getAddress());while(!messageQueue.isEmpty()){Msg message = messageQueue.poll();message.exec(abonent);}}

Address Service Message System Account Frontend

frontendaccount serviceget Id from StorageMsgGetUserIdMsgUpdateUserIdHello user!submit namewait for authcheck statewait for authcheck state

run()public void run() {while (true) {messageSystem.execForAbonent(this);Thread.sleep(TICK_TIME);}}

chibrikov@corp.mail.ru