1 37 package com.sun.j2ee.blueprints.activitysupplier.pomessagebean; 38 import javax.ejb.*; 39 import javax.jms.*; 40 import javax.xml.rpc.*; 41 import javax.naming.*; 42 43 import com.sun.j2ee.blueprints.activitysupplier.JNDINames; 44 import com.sun.j2ee.blueprints.activitysupplier.powebservice.*; 45 import com.sun.j2ee.blueprints.activitysupplier.purchaseorder.ejb.*; 46 import com.sun.j2ee.blueprints.servicelocator.*; 47 import com.sun.j2ee.blueprints.servicelocator.ejb.*; 48 49 public class ActivityMessageBean implements 50 MessageDrivenBean, MessageListener { 51 52 private transient MessageDrivenContext mdc = null; 53 54 57 public ActivityMessageBean() {} 58 59 62 public void setMessageDrivenContext(MessageDrivenContext mdc) { 63 this.mdc = mdc; 64 } 65 66 69 public void onMessage(Message message) { 70 ActivityOrder ao = null; 71 72 try { 73 String messageID = message.getJMSMessageID(); 74 if (message instanceof ObjectMessage) { 75 ObjectMessage msg = (ObjectMessage)message; 76 ao = (ActivityOrder)msg.getObject(); 77 } else { 78 System.out.println("Wrong type message: " 79 + message.getClass().getName()); 80 } 81 } catch (JMSException e) { 82 e.printStackTrace(); 85 } 86 87 try { 88 doWork(ao); 89 } catch (OrderSubmissionException oe) { 90 oe.printStackTrace(); 93 } 94 95 } 96 97 private void doWork(ActivityOrder act) throws OrderSubmissionException { 98 try { 99 persistOrder(act); 100 } catch (Exception e) { 101 e.printStackTrace(); 104 } 105 sendInvoice(act); 106 } 107 108 private void sendInvoice(ActivityOrder act) { 109 Invoice inv = new Invoice("1234", act.getOrderId(), "ACTIVITY_INVOICE", 110 act, "COMPLETED"); 111 try { 112 InitialContext ic = new InitialContext(); 113 WebServiceBroker svc = (WebServiceBroker) 114 ic.lookup(JNDINames.BROKER_SERVICE_NAME); 115 String endpointURI = (String ) 116 ic.lookup(JNDINames.BROKER_SERVICE_URL); 117 BrokerServiceIntf port= (BrokerServiceIntf) 118 svc.getPort(BrokerServiceIntf.class); 119 120 ((Stub)port)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, 122 endpointURI); 123 port.submitDocument(inv.toXML()); 124 } catch (Exception ne) { 125 ne.printStackTrace(); 128 } 129 } 130 131 134 public void persistOrder(ActivityOrder act) 135 throws OrderSubmissionException { 136 137 try { 138 ServiceLocator sl = new ServiceLocator(); 139 140 ActivityPurchaseOrderLocalHome actLocalHome = 141 (ActivityPurchaseOrderLocalHome) 142 sl.getLocalHome(JNDINames.ACTIVITY_PURCHASEORDER_EJB); 143 ActivityPurchaseOrderLocal actLocal = 144 (ActivityPurchaseOrderLocal) actLocalHome.create(act); 145 } catch (ServiceLocatorException je) { 146 throw new OrderSubmissionException("Error while persisting order:" 147 + je.getMessage()); 148 } catch(CreateException ce) { 149 throw new OrderSubmissionException("Error while persisting order:" 150 + ce.getMessage()); 151 } 152 } 153 154 155 158 public void ejbCreate() {} 159 160 163 public void ejbRemove() { 164 mdc = null; 165 } 166 } 167 | Popular Tags |