lundi 29 août 2011

Walkthrough: Extending RunBaseBatch Class to Create and Run a Batch [AX 2012]


[This documentation content is for preview only, and has not been fully updated for Microsoft Dynamics AX 2012.]
You can design your own batch job by extending the RunBaseBatch class. You can also write code to schedule the batch to run.
By default, after a batch is scheduled, the batch is no longer associated with the client. The batch runs on the Application Object Server (AOS). However, you have the option to make a batch run on a client.
This walkthrough illustrates the following tasks:
In this procedure you will configure your AOS to be a batch server.

To configure your AOS batch server

  1. In the Navigation Pane, click Administration, expand Setup, and then click Server configuration.
  2. In the Server configuration form, click the Overview tab, and then click the row that has your AOS listed.
  3. Select the check box in the column Is Batch Server.
  4. On the Batch server schedule tab, make sure that the server can process at least a few concurrent threads (eight is a nice default). Ensure the start-to-end time range is broad enough to include the time when you want to test.
  5. Select the Batch server groups tab. Make sure that the Empty batch group is in the Selected groups list.
  6. Close the form.
In this procedure, you will extend the RunBaseBatch class. Objects of type RunBaseBatch implement all the methods that the system needs in order to run the object as a batch job. You will extend RunBaseBatch to override the run method. The run method contains the logic that handles the particular processing need that you are solving.

To extend RunBaseBatch

  1. Press Ctrl+D to open the Application Object Tree (AOT) of the client.
  2. Right-click the Classes node, and then select New Class.
  3. Expand the node for your new class. Rename it Batch4DemoClass.
  4. Right-click the classDeclaration node under Batch4DemoClass, and then click Edit.
  5. In the code Editor window, after Batch4DemoClass, add extends RunBaseBatch. Save the change.
  6. In the AOT right-click the Batch4DemoClass node, select Override Method, and then select the run method. Edit the run method to match the code shown in the following example.
  7. Override the methods pack and unpack. Edit them to match the code shown in the following example.
    NoteNote
    For this example, do not yet override the runsImpersonated method. The base method always returns true. true means the batch must run under the authority of the person who scheduled the batch, and that no client session is involved.

    class Batch4DemoClass extends RunBaseBatch
    {
    public void run()
    {
    ;
    // The purpose of your job.
    info(strFmt("Hello from Batch4DemoClass .run at %1"
    ,DateTimeUtil ::toStr(
    DateTimeUtil ::utcNow())
    ));
    }
    public container pack()
    {
    return conNull();
    }
    public boolean unpack(container packedClass)
    {
    return true;
    }
    }
In this procedure, you will write an X++ job that can be run to schedule an occurrence of your batch. Then you will run the job.

To schedule your batch job

  1. In the AOT, right-click the Jobs node, and then select New Job.
  2. In the Editor window, enter the X++ code shown in the following example, and then save the changes.
  3. In the Editor window, press F7 to compile your job, and then press F5 to run your job.
    NoteNote
    The result of your job is that your batch is scheduled to run.

    static void Job_ScheduleBatch2(Args _args)
    {
    BatchHeader batHeader;
    BatchInfo batInfo;
    RunBaseBatch rbbTask;
    str sParmCaption = "My Demonstration (b351)";
    ;
    rbbTask = new Batch4DemoClass();
    batInfo = rbbTask .batchInfo();
    batInfo .parmCaption(sParmCaption);
    batInfo .parmGroupId(""); // The "Empty batch group".
    batHeader = BatchHeader ::construct();
    batHeader .addTask(rbbTask);
    batHeader .save();
    info(strFmt("'%1' batch has been scheduled.", sParmCaption));
    }
In this procedure, you will use the Navigation Pane to view the line item that represents your scheduled batch.

To view your scheduled batch

  1. In the Navigation Pane, click Basic, expand Inquiries, and then click Batch Job.
  2. On the Batch job form , determine the status of your batch by looking in the Status column. After the status becomes Ended, click the Batch job history button.
  3. On the Batch job history form, click the Log button. This displays the Infolog form that shows the output message from the Batch4DemoClass .run method.
    The following are the actual output messages that are captured by the Infolog:

    Message_@SYS14327 (02:12:57 pm)
    Hello from Batch4DemoClass .run, at 2008-01-25T22:05:48
    Infolog for Job My Demonstration (b351) (5637144579)
    Infolog for Task My Demonstration (b351) (5637144579)
    Hello from Batch4DemoClass .run, at 2008-01-25T22:05:48
In this procedure, you will add an override of the runsImpersonated method to your Batch4DemoClass class. Your override method must return false. The value false means that the system cannot run the batch by having it impersonate a logged on session. Instead, the system must find the client session of the user before the system can run the batch (on the server, as always). The client session can exist on a different computer from where the AOS is running.

To make your batch dependent on a client session

  1. Override the runsImpersonated method of your Batch4DemoClass class. The override code is shown in the example.
  2. Rerun your job ScheduleMyBatchJob_Job.
  3. View your batch on the Batch job form.

    public boolean runsImpersonated()
    {
    // false means that the batch must run on a client.
    return false;
    }
You have a batch class that overrides the runImpersonated method. The override makes the batch eligible to run only on a client computer. You run a job that schedules the batch. The batch remains in a waiting status until you use the client to tell the AOS to send the client-bound batch to your client now so the batch can run.
In this procedure, you will use the Navigation Pane to tell the AOS to send client-bound batches to your client.

To run your batch on the client

  1. In the Navigation Pane, click Basic, expand Periodic, expand Batch, and then click Processing. This displays the form Set up batch processing.
  2. Click the drop-down list, and then select Empty batch group. Batch tasks in this group will be run.
  3. Make sure the check box labeled Private is clear.
  4. Click OK to start the Batch processing form.

Aucun commentaire:

Enregistrer un commentaire