1 2 3 package Jt.examples.patterns; 4 5 import java.util.*; 6 import java.lang.reflect.*; 7 import java.beans.*; 8 import java.io.*; 9 import Jt.*; 10 11 14 15 public class Colleague extends JtThread { 16 17 Object mediator; 18 String greetingMessage = "Hi there "; 19 20 21 26 27 public void setGreetingMessage (String greetingMessage) { 28 this.greetingMessage = greetingMessage; 29 30 } 31 32 35 36 public String getGreetingMessage () { 37 return (greetingMessage); 38 } 39 40 41 46 47 public void setMediator (Object mediator) { 48 this.mediator = mediator; 49 50 } 51 52 55 56 public Object getMediator () { 57 return (mediator); 58 } 59 60 61 public Colleague () { 62 } 63 64 65 67 private void sleep (long period) { 68 69 try { 70 Thread.sleep (period); 71 } catch (Exception e) { 72 handleException (e); 73 } 74 75 } 76 77 83 84 85 public Object processMessage (Object message) { 86 87 String msgid = null; 88 JtMessage e = (JtMessage) message; 89 Object content; 90 Object data; 91 JtMessage tmp; 92 93 94 95 if (e == null) 96 return null; 97 98 msgid = (String ) e.getMsgId (); 99 100 if (msgid == null) 101 return null; 102 103 content = e.getMsgContent(); 104 data = e.getMsgData (); 105 106 if (msgid.equals ("JtREMOVE")) { 108 return (this); 109 } 110 111 112 if (msgid.equals ("JtACTIVATE")) { 113 114 if (mediator == null) { 115 handleError ("the mediator attribute needs to be set"); 116 return (null); 117 } 118 119 120 122 tmp = new JtMessage ("JOIN"); 123 tmp.setMsgFrom (this); 124 sendMessage (mediator, tmp); 125 126 128 tmp = new JtMessage ("MESSAGE"); 129 tmp.setMsgContent (greetingMessage); 130 tmp.setMsgFrom (this); 131 sendMessage (mediator, tmp); 132 133 135 sleep (3000L); 136 137 139 tmp = new JtMessage ("EXIT"); 140 tmp.setMsgFrom (this); 141 sendMessage (mediator, tmp); 142 143 return (this); 144 } 145 146 return (super.processMessage (message)); 147 148 149 } 150 151 152 153 154 } 155 156 157 | Popular Tags |