处理消息有效内容

可以在以前存在的调解处理程序中处理消息有效内容以及将消息有效内容从一种消息格式转换为另一种消息格式。

开始之前

在 EJB 项目中创建或打开调解处理程序。有关更多信息,请参阅编写调解处理程序。还应该阅读主题调解编程的编码技巧中关于成功编写调解的提示。

关于此任务

可以执行此任务以对消息有效内容完成以下某些或全部操作:
  • 找到消息有效内容中的数据对象
  • 将有效内容转换为另一种格式
  • 例如,如果希望调解记录消息,请将有效内容转换为字节数组。
要处理消息的内容,请使用 SIMessage 和 SIMessageContext API。或者,使用 SIMediationSession 来使调解能够访问服务集成总线以发送和接收消息。有关更多信息,请参阅:

要处理消息中的特定字段,请使用服务数据对象 (SDO) V1 数据图。有关更多信息,请参阅SDO 数据图。有关受支持消息类型的格式的更多信息以及如何处理这些消息类型的示例,请参阅Web Service 消息的 SDO 数据图映射

要处理消息有效内容,请执行以下步骤:

过程

  1. 在调解处理程序的方法 handle (MessageContext context) 中找到要插入功能性调解代码的位置。 接口为 MessageContext,除非您仅希望使用 MessageContext 提供的方法,否则应该将此接口强制转换为 SIMessageContext。
  2. 检索消息有效内容的数据图,如下所示:
    1. 从 MessageContext 对象中获取 SIMessage。 例如:
      SIMessage message = ((SIMessageContext)context).getSIMessage();
    2. 获取消息格式字符串以确定其类型: 例如:
      String messageFormat = message.getFormat();
    3. 从消息中检索 DataGraph 对象。 例如:
      DataGraph dataGraph = message.getDataGraph();
      有关更多信息,请参阅SDO 数据图
  3. 可选: 找到有效内容中的数据对象:
    1. 浏览该数据图以找到指定的 DataObject。 例如,在以下示例中,DataObject 的名称为“data”:
      DataObject dataObject = dataGraph.getRootObject().getDataObject("data");
    2. 检索数据对象中包含的信息。 例如,如果消息是文本消息:
      String textInfo = dataObject.getString("value");
  4. 使用消息中的字段。 要了解如何执行此操作的示例,请参阅消息字段的示例代码
  5. 可选: 将有效内容转换为另一种格式:
    1. 查看在消息格式之间进行代码转换主题以了解对有效内容进行格式转换的含意。
    2. 调用 getNewDataGraph 方法并以参数的形式传递新格式,该方法将以新格式返回有效内容的副本。 例如:
      DataGraph newDataGraph = message.getNewDataGraph(newFormat);
    3. 使用 setDataGraph 方法将采用新格式的数据图写回到消息中。 例如:
      message.setDataGraph(newDataGraph, newFormat);
  6. 可选: 将有效内容转换为字节流:
    1. 查看主题对消息有效内容进行代码转换来将其转换为字节数组对字节数组进行代码转换来将其转换为消息有效内容,以了解在消息格式与字节流之间来回转换的含意。
    2. 调用 getDataGraphAsBytes 方法,该方法将以字节流的形式返回有效内容的副本。 例如:
      byte[] newByteArray = message.getDataGraphAsBytes();
    3. 调用 SIDataGraphFactory API 提供的 createDataGraph 方法,该方法根据传递到它的格式来解析字节以创建新的数据图。 例如:
      DataGraph newDataGraph = SIDataGraphFactory.getInstance().createDataGraph( byteArray, format);
    4. 将消息作为字节流进行处理。 要了解如何执行此操作的示例,请参阅消息字段的示例代码
  7. 在调解代码中返回 True,以便将 MessageContext 传递到处理程序列表中的下一个调解处理程序。 如果返回值是 False,那么将废弃 MessageContext,并且不会将其传递到目标。
    注: 如果调解处理程序是处理程序列表中的最后一个处理程序,并且转发路由路径为空,那么该目标上的使用应用程序可以使用此消息。如果转发路由路径不为空,那么该目标上的任何使用者都无法使用此消息。相反,此消息将转发到路由路径中的下一个目标。

消息字段的示例代码

public boolean handle(MessageContext context) throws MessageContextException {
	
			/* Get the SIMessage from the MessageContext object */
		SIMessage message = ((SIMessageContext)context).getSIMessage(); 
	
		/* Get the message format string */
		String messageFormat = message.getFormat(); 
	
		/* If you have a JMS TextMessage then extract the text contained in the message.	*/
		if(messageFormat.equals("JMS:text"))
	{
				/* Retrieve the DataGraph object from the message */
				DataGraph dataGraph = message.getDataGraph();
		
				/* Navigate down the DataGraph to the DataObject named 'data'. */
				DataObject dataObject = dataGraph.getRootObject().getDataObject("data");
		
				/* Retrieve the text information contained in the DataObject. */
				String textInfo = dataObject.get("value");
		
				/* Use the text information retrieved */
		System.out.println(textInfo);
	}
	
	
		/* Return true so the MessageContext is passed to any other mediation handlers
		* in the handler list	*/
			return true;
	
	}
用于将消息有效内容作为字节流进行处理的完整调解功能代码可能类似于以下示例:
  public boolean handle(MessageContext context)throws MessageContextException {
  
				/* Get the SIMessage from the MessageContext object */
			SIMessage message = ((SIMessageContext)context).getSIMessage(); 
    
    if (!SIApiConstants.JMS_FORMAT_MAP.equals(msg.getFormat()))
    {
      try
      {
        dumpBytes(msg.getDataGraphAsBytes());
      }
      catch(Exception e)
      {
        System.out.println("The message contents could not be retrieved due to a "+e);
      }
    }
    else
    {
      System.out.println("The bytes for a JMS:map format message cannot be shown.");
    }
    
    		return true;
  }
  
  private static void dumpBytes(byte[] bytes)
  {
    // Subroutine to dump the bytes in a readable form to System.out
  }
}

指示主题类型的图标 任务主题



时间戳记图标 最近一次更新时间: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tjy1501
文件名:tjy1501.html