+ Reply to Thread
Results 1 to 5 of 5

Thread: Single Client executing Multiple Services

  1. #1
    lechen's Avatar
    lechen is offline Junior Member
    Join Date
    March 12th, 2008
    Location
    Toronto, Ontario
    Posts
    71
    Blog Entries
    1
    Downloads
    8
    Uploads
    0

    Default Single Client executing Multiple Services

    1. Objective

    I have two different service implementations for my application:
    • One short running service generating output results
    • One longer running service using result returned from first service as input
      message
    My client needs to execute both of them sequentially.




    2. Configuration

    In application profile:
    • Add a second SessionType named LongRunningTasks
    • Add a second Service named SampleService2 assigned to the new SessionType, by setting serviceName="SampleService2"
    • Set a different packageName for the new SessionType. This package will contain the second service binary to be executed.
    • Define SampleServiceCPP to be default service. Session Type ShortRunningTasks uses default service.
    • Optionally, define maxOtherInstances="1" for both services, to avoiding frequent shut down and restart of the SIs. Refer to Application Profile Reference for details.
    • Optionally, configure the Session Types. To demonstrate, set recoverable=true for LongRunningTasks.
    Code:
    <SessionTypes>
        <Type name="ShortRunningTasks" priority="1" recoverable="false"
            sessionRetryLimit="3" taskRetryLimit="3"
            abortSessionIfTaskFail="false" abortSessionIfClientDisconnect="true"
            suspendGracePeriod="100"  taskCleanupPeriod="100"/>
    </SessionTypes>
    
    <SessionTypes>
        <Type name="LongRunningTasks" priority="1" recoverable="true"
            serviceName="SampleService2" sessionRetryLimit="3" taskRetryLimit="3"
            abortSessionIfTaskFail="false" abortSessionIfClientDisconnect="true"
            suspendGracePeriod="100"  taskCleanupPeriod="100"/>
    </SessionTypes>
    
    <Service maxOtherInstances="1" default="true" description="The Sample Service"
        name="SampleService" packageName="SampleServiceCPP">
        ....
    </Service>
    
    <Service maxOtherInstances="1" description="The Sample Service"     
        name="SampleService2" packageName="SampleServiceCPP2">
        ....
    </Service> 

    3. Code

    In client application (I used samples/CPP/SampleApp/SyncClient for demonstration):
    • Save output result from first session (1 task)
    • Create a second session with its own attributes
    • Submit 1 task for this session
    • Fetch task result and display output
    Code:
    // Now get our results - will block here until all tasks retrieved
    EnumItemsPtr enumOutput = sesPtr->fetchTaskOutput(tasksToSend);
    
    std::string str = "";
    
    // Inspect results
    TaskOutputHandlePtr output;
    while(enumOutput->getNext(output))
    {
        // Check for success of task
        if (true == output->isSuccessful())
        {
            // Get the message returned from the service
            MyMessage outMsg;
            output->populateTaskOutput(&outMsg);
    
            // Display content of reply
            cout << "Task Succeeded [" <<  output->getId() << "]" << endl;
            cout << "Integer Value : " << outMsg.getInt() << endl;
            cout << outMsg.getString() << endl;
    
            // Create message for second session, using output from first
            str += outMsg.getString();
        }
        else
        {
            // Get the exception associated with this task
            SoamExceptionPtr ex = output->getException();
            cout << "Task Failed : " << ex->what() << endl;
        }
    }
    
    // After result retrieved from first session,
    // set up session creation attributes for second session
    SessionCreationAttributes attributes2;
    attributes2.setSessionName("mySession");
    attributes2.setSessionType("LongRunningTasks");
    attributes2.setSessionFlags(Session::ReceiveSync);
    
    // Create a second synchronous session
    SessionPtr sesPtr2 = conPtr->createSession(attributes2);
    
    // Retrieve and print session ID
    cout << "Session ID:" << sesPtr2->getId() << endl;
    
    int tasksToSend2 = 1;
    for (int taskCount = 0; taskCount < tasksToSend2; taskCount++)
    {
        MyMessage inMsg2(taskCount, true, const_cast<char*>(str.c_str()));
    
        //  Create task attributes
        TaskSubmissionAttributes attrTask2;
        attrTask2.setTaskInput(&inMsg2);
    
        // send it
        TaskInputHandlePtr input2 = sesPtr2->sendTaskInput(attrTask2);
    
        // Retrieve and print task ID
        cout << "task submitted with ID : " << input2->getId() << endl;
    }
    
    // Now get our results - will block here until all tasks retrieved
    EnumItemsPtr enumOutput2 = sesPtr2->fetchTaskOutput(tasksToSend);
    
    // Inspect results
    ... 

    In service application (I used samples/CPP/SampleApp/Service for demonstration):
    • For the first service (SampleServiceCPP), use existing Sampple Service code
    • For the second service (SampleServiceCPP2), modify output message and insert sleep() to differentiate between the 2 services
    Code:
    virtual void onInvoke(TaskContextPtr& taskContext)
    {
        /********************************************************************
        * Do your service logic here. This call applies to each task
        * submission.
        ********************************************************************/
    
        // Get the input that was sent from the client
        MyMessage inMsg;
        taskContext->populateTaskInput(inMsg);
    
        // We simply echo the data back to the client
        MyMessage outMsg;
        outMsg.setInt(inMsg.getInt());
    
        std::string str = "you sent : ";
        str += inMsg.getString();
        str += "\nwe replied : Hello Client !! This is Service 2\n>>> ";
        sleep(1000);
        
        ...
    } 

    4. Service Deployment

    Deploy the second service, with packageName matching the one defined in application profile:

    Code:
    lechen@ib08b05-930: soamdeploy view
    PACKAGE             APPLICATION   CONSUMER        CREATED TIME                 
    SampleServiceCPP    SampleAppCPP  /SampleAppCPP   Thu May  1 16:11:07 2008
    SampleServiceCPP2   SampleAppCPP  /SampleAppCPP   Sun May  4 02:04:18 2008 

    5. Execution

    Submit the client:
    • Second session used output from first session as input
    • Second session received output from second service
    Code:
    lechen@ib08b05-1051: Output/SyncClient
    connection ID=47f87058-0000-1000-c000-00112529883c-42851248-7361
    Session ID:907
    task submitted with ID : 1
    Task Succeeded [1]
    Integer Value : 0
    you sent : Hello Grid !!
    we replied : Hello Client !!
    >>> Synchronously.
    
    Session ID:908
    task submitted with ID : 1
    Task Succeeded [1]
    Integer Value : 0
    you sent : you sent : Hello Grid !!
    we replied : Hello Client !!
    >>> Synchronously.
    
    we replied : Hello Client !! This is Service 2
    >>> Synchronously.
    
    All Done !! 
    Observe that if we defined maxOtherInstances="1", the SIM keeps both SIs running concurrently. When either services need to be executed again, it will not have to be re-started.

    Code:
    lechen 10959 /opt/wa/4.0/linux2.4-glibc2.3-x86/etc/ssm -i ae317250-ffff-ffff-c000-00112529883c-104934320-7361 -u ib08b05.lsf.platform.com:52595 -a SampleAppCPP
    lechen 10973 /opt/wa/4.0/linux2.4-glibc2.3-x86/etc/sim -u ib08b05.lsf.platform.com:56719 -i dc435780-ffff-ffff-c000-00112529883c-202804144-10959 -a SampleAppCPP
    lechen 10979 /opt/wa/deploy/SampleAppCPP/SampleServiceCPP.v1/SampleServiceCPP
    lechen 10991 /opt/wa/deploy/SampleAppCPP/SampleServiceCPP2.v3/SampleServiceCPP 

    6. Reference Document

    Development Guide - Run multiple services in an application


    Comments welcomed.
    Attached Files
    Last edited by lechen; May 5th, 2008 at 08:52 PM.

  2. #2
    Young's Avatar
    Young is offline Junior Member
    Join Date
    March 5th, 2008
    Location
    Toronto, Canada
    Posts
    58
    Blog Entries
    1
    Downloads
    7
    Uploads
    0

    Default Review

    Pretty interesting article demonstrating the way to make Sym application more multilateral.
    Two quick question:
    1) Why do you normally set recoverable to be false for ShortRunningTasks session type and the opposite for LongRunningTasks?
    2) Can you explain the attribute maxOtherInstances in a little more detail?

  3. #3
    Ajith's Avatar
    Ajith is offline Symphony DE Moderator
    Join Date
    February 28th, 2008
    Location
    Markham, Ontario
    Posts
    104
    Blog Entries
    2
    Downloads
    10
    Uploads
    0

    Default

    Quote Originally Posted by Young View Post
    2) Can you explain the attribute maxOtherInstances in a little more detail?
    maxOtherInstances allows you to define the number of service instances a specific SIM allows to stay running before it starts the specified service. Say for instance you have one slot on a host -> so one SIM. There is one service binary that needs a lot of memory. Set maxOtherInstances = 0, and the SIM will stop all the running service instances prior to starting the service instance requiring all the memory. On the other hand, if all the services associated with this application can run concurrently, set maxOtherInstanes >= # of services -1. The SIM will start a service instance when required, but will not stop any service instances. Setting maxOtherInstances this way improves performance in applications that can reference any service concurrently.

  4. #4
    lechen's Avatar
    lechen is offline Junior Member
    Join Date
    March 12th, 2008
    Location
    Toronto, Ontario
    Posts
    71
    Blog Entries
    1
    Downloads
    8
    Uploads
    0

    Default

    Quote Originally Posted by Young View Post
    1) Why do you normally set recoverable to be false for ShortRunningTasks session type and the opposite for LongRunningTasks?
    That was just to demonstrate the flexibility that we can configure these session types differently, as apply to the different services.

    For recoverable, user might want to recover LongRunningTasks because they are more expensive to re-run, compred to ShortRunningTasks

    Quote Originally Posted by Young View Post
    2) Can you explain the attribute maxOtherInstances in a little more detail?
    Copied the following from application profile reference guide, which I've linked to in the article:

    maxOtherInstances

    Applies only when multiple services are used in a single application. Defines the maximum number of other service instances that a single service instance manger can run concurrently, in addition to the current service.

    When this parameter is set to 0, no other service instances started by the service instance manager can run at the same time when this service instance manager starts this service. As a result, any other running service instances managed by this service instance manager are stopped when this service is started.

    When this parameter is set to a number larger than 0, the service instance manager can keep maxOtherInstances service instances running along with this service concurrently. Setting a value larger than 0 saves service instance loading time, because service instances are not shut down and restarted.

  5. #5
    Bart Gridson is offline Junior Member
    Join Date
    May 9th, 2008
    Posts
    9
    Downloads
    0
    Uploads
    0

    Default

    Definining maxOtherInstances is not required for all scenarios.

    Applicable when:
    Multiple services need to be invoked frequently with considerable startup cost.

    Not applicable when:
    Service not involked frequently and occupies system resources.

    In either case:
    The service that is most frequently used should be set as the Default service (default="true"). If this service has a high startup cost, consider pre-starting it as soon as the application is enabled (preStartApplicaton="true").

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts