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

  • View
    179

  • Download
    3

Embed Size (px)

Transcript

  • 1. Java 3 Message System

2. 1. Frontend Account Service 2. Concurrent Collections 3. Message System2 3. UserSession , - UserSession UserSession sessionId ( HttpSession) UserSession : String sessionId String userName Long userId Frontend- Map sessionIdToSession; 3 4. Id c Accounter, handle() Accounter userId Accounter c 4 5. 5 6. Frontend Account Service Accounter Frontend Account Service Frontend Frontend c Frontend Account Service , Frontend 6 7. 0: . 0: sessionId + 1: sessionId + AccountServer 1: sessionId + 2: sessionId 2: sessionId + 3: sessionId 3: sessionId + + userName + Id: + userId 7 8. 2 Frontend Account Service8 9. 1. Frontend Account Service 2. Concurrent Collections 3. Message System9 10. Atomic java.util.concurrent.atomicAtomicBoolean AtomicInteger synchronized optimistic lockingAtomicLong public final int incrementAndGet() { while (true) { int current = get(); //get() (volatile) int next = current + 1; if (compareAndSet(current, next)) return next; } } public final boolean compareAndSet(int expect, int update) { return unsafe.compareAndSwapInt(this, valueOffset, expect, update); } 10 11. Synchronized vs. Concurrent Concurrent , ( synchronized)ConcurrentHashMap concurrent java.util.Hashtable synchronizedsynchronized , Concurrent 11 12. Concurrent Collections CopyOnWriteArrayList ArrayListCopyOnWriteArraySetSet CopyOnWriteArrayListConcurrentHashMapthread safe HashMapConcurrentSkipListMap ConcurrentSkipListSetset ConcurrentSkipListMap12 13. Concurrent Queues ConcurrentLinkedQueuethread safe BlockingQueue ArrayBlockingQueue LinkedBlockingQueue BlockingDeque ArrayBlockingDeque 13 14. 1. Frontend Account Service 2. Concurrent Collections 3. Message System14 15. Thread-Safe Thread-local 15 16. Message System MessageSystem 16 17. MsgToAccountServiceFrontendFrontend QueueMsgToFrontendAccount QueueAccount ServiceMessageSystem17 18. Address Abonent public class Address { static private AtomicInteger abonentIdCreator = new AtomicInteger(); final private int abonentId; public Address(){ this.abonentId = abonentIdCreator.incrementAndGet(); } public int hashCode() { return abonentId; } } public interface Abonent { Address getAddress(); } 18 19. Message public 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);} 19 20. Message to Account Service public abstract class MsgToAS extends Msg{ public MsgToAS(Address from, Address to) { super(from, to); } void exec(Abonent abonent) { if(abonent instanceof AccountService){ exec((AccountService) abonent); } } abstract void exec(AccountService accountService);}20 21. Message to Account Service public class MsgGetUserId extends MsgToAS { private String name; private String sessionId;public MsgGetUserId(Address from, Address to, String name, String sessionId) { super(from, to); this.name= name; this.sessionId = sessionId; } void exec(AccountService accountService) { Long id = accountService.getUserId(name); Msg back = new MsgUpdateUserId(getTo(), getFrom(), sessionId, id); accountService.getMessageSystem(). sendMessage(back); } } 21 22. Msg - Address from - Address toMsgToASMsgToFrontendMsgGetUserIdMsgUpdateUserId- String name - Integer sessionId- Integer sessionId - Integer userId 22 23. Message System private 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); } } 23 24. MessageSystem Frontend AccountService MessageSystem Address, Abonent Msg 24 25. Address Service Message System Account Frontend , 25 26. Address Service public class AddressService { private Address accountService; public Address getAccountService() { return accountService; } public void setAccountService(Address accountService) { this.accountService = accountService; }}AddressService MessageSystem Address UserSession26 27. 27 28. run() public void run() { while (true) { messageSystem.execForAbonent(this); Thread.sleep(TICK_TIME); } }28 29. 29 30. chibrikov@corp.mail.ru