Chinaunix首页 | 论坛 | 博客
  • 博客访问: 464195
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 13:30:23


  我听说许多人(包括“重量级”人物Don Box)错误地认为,JBI“包含EJB功能”,又或者是JAX-WS或 Web services (JSR 181)。
  
  我可以理解他们弄错的原因。JBI的提出者将其描述为企业服务总线(ESB)的标准,这样就很容易推出一个“显而易见”的结论:JBI必定提供了一种使服务在服务总线上可用的便捷方法。这其实是不正确的。JBI实际是处于一个中间层。但它不是插在服务中,而是插在服务引擎中,它反过来又允许人们在其中插入服务。这里有着微妙的区别,因为可以驻留多个服务就也可以提供单一的服务。James Strachan写了一篇非常不错的文章,简要介绍了JBI。而本文的目的则是要澄清对JBI的误解。
  
  我们来看一下如何通过JBI使普通的HelloWorld web服务可用。
  
  下面的代码是一个JSR181web服务:
  
  @WebServiceclass MyService {String greeting(String input) {return "Hello "+input;}}
  
  如果您使用JBI服务引擎实现服务,您的代码将会类似于下面所示的我已经使其尽可能简单了,(不过当然要在JBI模型的约束下):
  
  class MyServiceEngine implements Component, ComponentLifeCycle, Runnable {
  DeliveryChannel channel;
  ServiceEndpoint serviceEndpoint;
  
  public void init(ComponentContext context)
  throws JBIException
  {
  // Obtain reference to delivery channel
  channel = context.getDeliveryChannel();
  // Activate service endpoint (SEVICE2,ENDPOINT2)
  serviceEndpoint = context.activateEndpoint(SERVICE2, ENDPOINT2);
  // Spawn a thread to listen for messages.
  new Thread(this);
  }
  
  /** Loop that listens for requests**/
  void run() {
  while (true) {
  MessageExchange exchange = channel.accept();
  if (exchange instanaceof InOut)
  process((InOut) exchange);
  else {
  // Don't know how to send an error to the sender.
  Log.log("I don't understand this: "+exchange);
  }
  }
  }
  
  /**Process one request and send the result **/
  void process(InOut inOut) {
  NormalizedMessage inMsg = inOut.getInMessage();
  NormalizedMessage outMsg = inOut.createMessage();
  
  try {
  // Get the input as a DOM Text node
  Node inNode = (Node)((DOMSource)outMsg.getContent ()).getNode();
  
  // perform appropriate processing to inMsg
  Node outNode = greet(inMsg);
  
  // populate message content
  outMsg.setContent(new DOMSource(outNode));
  
  // attach message to exchange
  inOut.setOutMessage(outMsg);
  }
  catch (Exception ex) {
  inOut.setError(ex);
  inOut.setStatus(ExchangeStatus.ERROR);
  }
  channel.send(inOut);
  }
  
  /** The real work **/
  Node greet(Node inNode) {
  String input = inNode.getTextContent();
  String result = "Hello "+input;
  Node outNode = inNode.getOwnerDocument().createTextNode (result);
  return outNode;
  }
  }
  
  显然,如果您的环境已经有了驻留web services,业务流程,XML变换和其他业务代码的引擎,那么就不适合使用JBI。
【责编:admin】

--------------------next---------------------

阅读(27) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~