Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Web services transactions

A practitioner's approach

Return to article

When you print this page, select the landscape layout option.


Listing 3. Client application
public class PersistDataClient extends HttpServlet
{

    private PersistDataOne_PortType persistOne;
    private PersistDataTwo_PortType persistTwo;
    ServletContext context;

    public PersistDataClient()
    {
    }

    public void init(ServletConfig config)
        throws ServletException
    {
        String baseParam = config.getInitParameter("baseURL");
        String baseURL;
        if(baseParam != null)
        {
            baseURL = baseParam.endsWith("/") ? baseParam : baseParam + "/";
        } else
        {
            baseURL = "http://localhost:8080/jboss-net/services/";
        }
        try
        {
            PersistDataOneServiceLocator 
            persistDataOneServiceLocator = new PersistDataOneServiceLocator();
            String persistDataOneWSDDServiceName = 
            	persistDataOneServiceLocator.getPersistDataOneWSDDServiceName();
            String persistOneUrl = baseURL + persistDataOneWSDDServiceName;
            persistOne = persistDataOneServiceLocator.getPersistDataOne(
            			new URL(persistOneUrl));
            PersistDataTwoServiceLocator persistDataTwoServiceLocator = 
            			new PersistDataTwoServiceLocator();
            String persistDataTwoWSDDServiceName = 
            	persistDataTwoServiceLocator.
            		getPersistDataTwoWSDDServiceName();
            String persistTwoUrl = baseURL + persistDataTwoWSDDServiceName;
            persistTwo = 
            	persistDataTwoServiceLocator.
            		getPersistDataTwo(new URL(persistTwoUrl));
        }
        catch(Exception e)
        {
            throw new ServletException(e.toString(), e);
        }
        context = config.getServletContext();
    }

    public void doGet(HttpServletRequest request, 
                        HttpServletResponse response)
        throws ServletException, IOException
    {
        boolean onePassFail = true;
        boolean twoPassFail = true;
        String result = "Transaction finished OK.";
        try
        {
            runBusinessActivity(onePassFail, twoPassFail);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        request.setAttribute("result", result);
    }

    private void runBusinessActivity(boolean onePassFail, 
                        boolean twoPassFail)
        throws Exception
    {
        UserBusinessActivity uba = 
                        UserBusinessActivityFactory.userBusinessActivity();
        if(uba == null)
        {
            System.out.println("uba is null");
        } else
        {
            uba.begin();
        }
        boolean isOK = false;
        try
        {
            if(persistOne.writeToFileOne(onePassFail) && 
                        persistTwo.writeToFileTwo(twoPassFail))
            {
                isOK = true;
            }
        }
        catch(Throwable th)
        {
            System.out.println("CLIENT: caught exception processing bookings, 
                        cancelling (" + th.getMessage() + ")");
        }
        if(isOK)
        {
            System.out.println("CLIENT: all OK");
            System.out.println("CLIENT: calling close on the transaction...");
            uba.close();
        } else
        {
            System.out.println("CLIENT: one or more services failed, 
                        calling cancel.");
            uba.cancel();
        }
        System.out.println("CLIENT: done.");
        System.out.flush();
    }
}

Return to article