1 24 25 package pushwithreturn; 26 27 import java.util.Collections ; 28 29 import org.objectweb.dream.AbstractComponent; 30 import org.objectweb.dream.PushWithReturn; 31 import org.objectweb.dream.control.activity.Util; 32 import org.objectweb.dream.control.activity.task.AbstractTask; 33 import org.objectweb.dream.control.activity.task.Task; 34 import org.objectweb.dream.message.Message; 35 import org.objectweb.dream.message.MessageType; 36 import org.objectweb.dream.message.MessageTypeImpl; 37 import org.objectweb.dream.message.manager.MessageManager; 38 import org.objectweb.fractal.api.Component; 39 import org.objectweb.fractal.api.NoSuchInterfaceException; 40 import org.objectweb.fractal.api.control.IllegalBindingException; 41 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 42 import org.objectweb.util.monolog.api.BasicLevel; 43 44 47 public class ProducerImpl extends AbstractComponent 48 { 49 50 PushWithReturn outPushWithReturnItf; 51 MessageManager messageManagerItf; 52 MessageType msgType = new MessageTypeImpl(TestChunk.DEFAULT_NAME, 53 TestChunk.TYPE); 54 55 Task tasks; 56 57 61 64 protected void beforeFirstStart(Component componentItf) 65 throws IllegalLifeCycleException 66 { 67 try 68 { 69 Util.addTask(componentItf, new ProducerTask("Producer task"), 70 Collections.EMPTY_MAP); 71 logger.log(BasicLevel.DEBUG, "task added"); 72 } 73 catch (Exception e) 74 { 75 throw new IllegalLifeCycleException("Can't add task"); 76 } 77 } 78 79 83 87 public synchronized void bindFc(String clientItfName, Object serverItf) 88 throws NoSuchInterfaceException, IllegalBindingException, 89 IllegalLifeCycleException 90 { 91 super.bindFc(clientItfName, serverItf); 92 if (clientItfName.equals(PushWithReturn.OUT_PUSH_WITH_RETURN_ITF_NAME)) 93 { 94 outPushWithReturnItf = (PushWithReturn) serverItf; 95 } 96 else if (clientItfName.equals(MessageManager.ITF_NAME)) 97 { 98 messageManagerItf = (MessageManager) serverItf; 99 } 100 } 101 102 105 public String [] listFc() 106 { 107 return new String []{PushWithReturn.OUT_PUSH_WITH_RETURN_ITF_NAME, 108 MessageManager.ITF_NAME}; 109 } 110 111 private class ProducerTask extends AbstractTask 112 { 113 114 private String message = "Producer"; 115 116 119 public ProducerTask(String name) 120 { 121 super(name); 122 } 123 124 127 public Object execute(Object hints) throws InterruptedException 128 { 129 try 130 { 131 Message msg = messageManagerItf.createMessage(msgType); 132 TestChunk chunk = (TestChunk) msg.getChunk(TestChunk.DEFAULT_NAME); 133 chunk.setMessage(message); 134 logger.log(BasicLevel.INFO, "Producer sends message " + msg); 135 Message returnMessage = outPushWithReturnItf.pushWithReturn(msg, null); 136 logger.log(BasicLevel.INFO, "Producer receives return message " 137 + returnMessage); 138 } 139 catch (Exception e) 140 { 141 e.printStackTrace(); 142 return STOP_EXECUTING; 143 } 144 Thread.sleep(1000); 145 return EXECUTE_AGAIN; 146 } 147 } 148 149 } | Popular Tags |