01: /*
02: * ChainBuilder ESB
03: * Visual Enterprise Integration
04: *
05: * Copyright (C) 2006 Bostech Corporation
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the
09: * Free Software Foundation; either version 2 of the License, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15: * for more details.
16: *
17: * You should have received a copy of the GNU General Public License along with
18: * this program; if not, write to the Free Software Foundation, Inc.,
19: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: *
21: *
22: * $Id: MessageReceiver.java 3688 2006-12-12 14:00:39Z tvolle $
23: */
24:
25: package com.bostechcorp.cbesb.runtime.component.bootstrap;
26:
27: import java.io.ByteArrayInputStream;
28:
29: import javax.jbi.component.ComponentContext;
30: import javax.jbi.messaging.ExchangeStatus;
31: import javax.jbi.messaging.MessageExchange;
32: import javax.jbi.messaging.MessagingException;
33: import javax.jbi.messaging.NormalizedMessage;
34: import javax.xml.transform.stream.StreamSource;
35:
36: import com.bostechcorp.cbesb.common.util.ErrorUtil;
37:
38: // receives outbound messages from the NMR
39:
40: public class MessageReceiver implements Runnable {
41: ComponentContext mContext = null;
42: boolean mIsRunning = true;
43:
44: public MessageReceiver(ComponentContext ctx) {
45: mContext = ctx;
46: }
47:
48: public void stopProcessing() {
49: mIsRunning = false;
50: }
51:
52: public void run() {
53: mainLoop: while (mIsRunning) {
54: try {
55: MessageExchange exchange = mContext
56: .getDeliveryChannel().accept(5000);
57: if (exchange != null) {
58: process(exchange);
59: }
60: } catch (MessagingException e) {
61: ErrorUtil.printError(
62: "\n\n\nInstallServer accept() got exception: ",
63: e);
64: continue mainLoop;
65: }
66: }
67: }
68:
69: private void process(MessageExchange exchange)
70: throws MessagingException {
71: if (exchange.getStatus() != ExchangeStatus.ACTIVE) {
72: return;
73: }
74: try {
75: String reply = "<hello>from InstallServer</hello>";
76: NormalizedMessage outMsg = exchange.createMessage();
77: StreamSource ss = new StreamSource(
78: new ByteArrayInputStream(reply.getBytes("utf-8")));
79: outMsg.setContent(ss);
80: exchange.setMessage(outMsg, "out");
81: mContext.getDeliveryChannel().send(exchange);
82: } catch (Exception e) {
83: ErrorUtil.printError("Exception in process(): ", e);
84: throw new MessagingException(e);
85: }
86:
87: }
88: }
|