Message patterns in java

Message Exchange Patterns in Web Services

A message Exchange Pattern (MEP) is a pattern for the exchange of messages between two communicating parties like a server and a client. The most common such pattern is the request-response pattern where the client sends a request and the server sends a response back. From a server perspective this is an input-output operation. Based on different combinations of input and output, the WSDL specification defines 4 patterns:

  • Input-Output Operations
  • Input-Only Operations.
  • Output-Input Operations.
  • Output-Only Operations.

Request-Response (Input – Output operation)

  • Client sends a request and server sends a response back; and is the most common one you can see.
  • The WSDL port receives a message and sends a correlated response back; and there will be an input message followed by output in WSDL.

One way input (Input only operation)

  • Client sends a message without expecting a response from the service. An example might be a heart beat server that listens to client ping at regular interval to see if it is up and running.
  • WSDL port only receives a message and there is an input message only in the WSDL

Solicit response (Output – input operation)

  • Server sends a message and client sends a response back mostly an acknowledgement or status update.
  • The WSDL port sends a message and receives a correlated message, and hence there is an output message followed by an input message in WSDL.

Notification or Publish-Subscribe(Output only operation)

  • Here server sends a message without expecting a response. Could be used in a publish subscribe scenario that connects a set of publishers to a set of subscribers.
  • The WSDL port sends a message without response, and hence there is an output message only in WSDL.
Читайте также:  Унарный оператор в javascript

Synchronous and asynchronous web service calls

  • Web service calls may be synchronous or asynchronous.
  • Synchnous calls usually follows the request-response message exchange pattern over HTTP.
    • Solicit response may also be done synchronous.
    • The client and the service may need to establish a mechanism to correlate the messages in case of asynchronous calls, as the response may be sent later through a callback mechanism implemented at the web service side.

    Quick Notes Finder Tags

    You are here

    Copyright © 2020, JavaJee.com.

    This is a personal technical blog where we share our understanding on various concepts and is neither an official page or documentation for the products described here, nor the official views of the companies we work with.

    Keywords used in this website are trademarks of their respective owners. This website is not affiliated with Oracle™ and/or any of the JEE frameworks like Spring™, Struts™, Hibernate™ and JSF™.

    All contents and materials are provided freely without any warranty or liability and nothing within the site should be considered as professional advice. In any doubt, please ask, and we will try to help you based on our knowledge. Please let us know if you feel anything is not right here (including any copyright violation) and we will act upon it as fast as we can.

    By using this site in any way, you agree to the terms of use and also confirm that you have read our copyright and trademark notice.

    Источник

    Dealing with Compound Messages

    A compound message may contain several kinds of variables: dates, times, strings, numbers, currencies, and percentages. To format a compound message in a locale-independent manner, you construct a pattern that you apply to a MessageFormat object, and store this pattern in a ResourceBundle .

    By stepping through a sample program, this section demonstrates how to internationalize a compound message. The sample program makes use of the MessageFormat class. The full source code for this program is in the file called MessageFormatDemo.java . The German locale properties are in the file called MessageBundle_de_DE.properties .

    1. Identify the Variables in the Message

    Suppose that you want to internationalize the following message:

    Notice that we’ve underlined the variable data and have identified what kind of objects will represent this data.

    2. Isolate the Message Pattern in a ResourceBundle

    Store the message in a ResourceBundle named MessageBundle , as follows:

    ResourceBundle messages = ResourceBundle.getBundle("MessageBundle", currentLocale);

    This ResourceBundle is backed by a properties file for each Locale . Since the ResourceBundle is called MessageBundle , the properties file for U.S. English is named MessageBundle_en_US.properties . The contents of this file is as follows:

    template = At on , \ we detected spaceships on \ the planet . planet = Mars

    The first line of the properties file contains the message pattern. If you compare this pattern with the message text shown in step 1, you’ll see that an argument enclosed in braces replaces each variable in the message text. Each argument starts with a digit called the argument number, which matches the index of an element in an Object array that holds the argument values. Note that in the pattern the argument numbers are not in any particular order. You can place the arguments anywhere in the pattern. The only requirement is that the argument number have a matching element in the array of argument values.

    The next step discusses the argument value array, but first let’s look at each of the arguments in the pattern. The following table provides some details about the arguments:

    Arguments for template in MessageBundle_en_US.properties

    Argument Description
    The time portion of a Date object. The short style specifies the DateFormat.SHORT formatting style.
    The date portion of a Date object. The same Date object is used for both the date and time variables. In the Object array of arguments the index of the element holding the Date object is 2. (This is described in the next step.)
    A Number object, further qualified with the integer number style.
    The String in the ResourceBundle that corresponds to the planet key.

    For a full description of the argument syntax, see the API documentation for the MessageFormat class.

    3. Set the Message Arguments

    The following lines of code assign values to each argument in the pattern. The indexes of the elements in the messageArguments array match the argument numbers in the pattern. For example, the Integer element at index 1 corresponds to the argument in the pattern. Because it must be translated, the String object at element 0 will be fetched from the ResourceBundle with the getString method. Here is the code that defines the array of message arguments:

    4. Create the Formatter

    Next, create a MessageFormat object. You set the Locale because the message contains Date and Number objects, which should be formatted in a locale-sensitive manner.

    MessageFormat formatter = new MessageFormat(""); formatter.setLocale(currentLocale);

    5. Format the Message Using the Pattern and the Arguments

    This step shows how the pattern, message arguments, and formatter all work together. First, fetch the pattern String from the ResourceBundle with the getString method. The key to the pattern is template . Pass the pattern String to the formatter with the applyPattern method. Then format the message using the array of message arguments, by invoking the format method. The String returned by the format method is ready to be displayed. All of this is accomplished with just two lines of code:

    formatter.applyPattern(messages.getString("template")); String output = formatter.format(messageArguments);

    6. Run the Demo Program

    The demo program prints the translated messages for the English and German locales and properly formats the date and time variables. Note that the English and German verbs («detected» and «entdeckt») are in different locations relative to the variables:

    currentLocale = en_US At 10:16 AM on July 31, 2009, we detected 7 spaceships on the planet Mars. currentLocale = de_DE Um 10:16 am 31. Juli 2009 haben wir 7 Raumschiffe auf dem Planeten Mars entdeckt.

    Источник

Читайте также:  Alhimikov net electron 01 html
Оцените статью