The ScheduledOrderService is the back-end service that polls the Order Repository at a periodic interval and submits scheduled Orders according to their schedules.

When an application that includes ATG Commerce deploys, a PlaceScheduledOrders task is scheduled with the /atg/dynamo/service/Scheduler. This scheduled task is run at the interval specified in ScheduledOrderService.schedule. When the scheduled task is run, the Scheduler calls ScheduledOrderService.performScheduledTask(). The ScheduledOrderService then attempts to obtain a global write lock from the server lock manager. If the lock is obtained successfully, then it calls doScheduledTask().

At a general level, the ScheduledOrderService.doScheduledTask() method polls the Order Repository for all scheduled Orders that should be checked out. For each scheduled Order it finds due for checkout, it then clones the template Order associated with the scheduled Order, checks out the cloned Order, and sets the nextScheduledRun property of the scheduled Order to the next date and time it should be checked out. If an error occurs when processing an individual scheduled Order, then a CommerceException is thrown, the state of the scheduled Order is set to Error, and the state of the cloned Order is set to PENDING_MERCHANT_ACTION. However, the remaining scheduled Orders are processed.

The following table describes the various methods of ScheduledOrderService that support this process:

Method

Description

performScheduledTask

Called when the Scheduler finds that the placeScheduledOrders task is scheduled to run. When this method is called, the ScheduledOrderService attempts to obtain a global write lock from the server lock manager. If the lock is obtained successfully, the ScheduledOrderService calls its own doScheduledTask() method.

The placeScheduledOrders task is run at the interval defined in ScheduledOrderService.schedule. While the schedule that you set depends on the business needs of your site, as a general rule, it is recommended that you set it to “every 1 day.” For information on how to specify a schedule, see the Scheduler Services section of the Core Dynamo Services chapter in the ATG Programming Guide.

Note: Be aware that the defined interval specified in ScheduledOrderService.schedule begins when an application that includes ATG Commerce deploys. Therefore, if, for example, the ATG Commerce server is redeployed every 11 hours, and if the schedule property of a given scheduled Order is set to “every 12 hours,” then the scheduled Order is never placed.

doScheduledTask

Calls processDueScheduledOrders() to process any scheduled Orders that need to be checked out. Other objects can call this method to trigger on demand a poll of the Order Repository for due scheduled Orders.

processDueScheduledOrders

Processes all scheduled Orders that need to be checked out.

This method calls pollForNewOrders() to retrieve the list of scheduled Orders that need to be checked out. It then iterates through the array and calls processDueScheduledOrder() on each scheduled Order. (See the methods described later in this table for details.)

pollForNewOrders

Uses the query defined in the ScheduledOrderService.pollQuery property to poll the Order Repository defined in the ScheduledOrderService.orderRepository property with the repository view defined in the itemDescriptorName property (by default, set to scheduledOrder). The method returns an array of scheduled Orders that need to be checked out.

processDueScheduledOrder

Processes the current scheduled Order.

This method calls placeScheduledOrder() to check out the scheduled Order. If the scheduled Order is successfully checked out, then the method calls getNextScheduledRun() to set its nextScheduledRun property to the next date and time that it should be checked out.

If the scheduled Order fails to be checked out, then it rolls back to its original state. It will be retrieved again for processing the next time that pollForNewOrders() is executed.

getNextScheduledRun

Sets the next time that the scheduled Order should be checked out.

placeScheduledOrder

This method checks out the current scheduled Order using the following process:

First, the template Order is retrieved by calling ScheduledOrderTools.getTemplateOrder() and passing in the current scheduled Order.

Second, the template Order is cloned by calling ScheduledOrderTools.cloneOrder().

Third, if the ScheduledOrderTools.repriceOnClone property is set to True, then the cloned order is repriced by calling ScheduledOrderTools.repriceScheduledOrder().

Fourth, the cloned Order is saved to the Order Repository in its present state by calling OrderManager.updateOrder(), which executes the updateOrder pipeline. (For more information on the updateOrder pipeline, see Updating an Order with the OrderManager in this chapter.)

Finally, the cloned Order is checked out by calling ScheduledOrderTools.processScheduledOrder(), which in turn calls OrderManager.processOrder() and passes in the cloned Order and pipeline to execute. The pipeline to execute is set in ScheduledOrderTools.processOrderChainId; by default, this property is set to processOrder. When the order has been checked out successfully, it is passed on to Fulfillment. (For more information on the processOrder pipeline, see Checking Out an Order in this chapter.)

repriceScheduledOrder

Reprices the cloned Order. The repriceScheduledOrder() method calls ScheduledOrderTools.repriceScheduledOrder(), which executes the repricing pipeline specified in ScheduledOrderTools.repriceOrderChainId. By default, this property is set to repriceOrder. (For more information on the repriceOrder pipeline, see the Commerce Processor Chains section.)

ATG Commerce provides an instance of class atg.b2bcommerce.order.scheduled.ScheduledOrderService, which extends atg.service.scheduler.SingletonSchedulableService. It is located in Nucleus at /atg/commerce/order/scheduled/ScheduledOrderService.

Class atg.service.scheduler.SingletonSchedulableService enables multiple ATG Commerce servers to run the same scheduled service, while guaranteeing that only one instance of the service will perform the scheduled task at any given time. This provides a degree of protection from server failures, since the loss of any single ATG Commerce server will not prevent the scheduled service from running on some other ATG Commerce server.

To use a SingletonSchedulableService like ScheduledOrderService on multiple ATG Commerce instances, you must run a server lock manager and point all client lock managers at it. For more information on SingletonSchedulableService, see the Scheduler Services section of the Core Dynamo Services chapter in the ATG Programming Guide.

Configuring a Scheduled Order Service to Handle Large Orders

As described at the beginning of this section (see Scheduling Recurring Orders), the scheduled order service is designed to run on only one machine at a time, and for that reason it is not well adapted to handle very large orders. If you do need to use it in situations where the number of scheduled orders is very high, you can do so by configuring multiple servers so that each one processes a specific subset of orders, thus balancing the load. To achieve this behavior, modify the pollQuery property of the ScheduledOrderService component so that it limits the order query to a subset of the available orders. (The pollQuery property is an instance of atg.repository.rql.RqlStatement. For information on setting it, see the ATG Repository Guide.) For example, you could set up one server to add a clause to the query so that it handles only orders where the profiles’ last names begin with A-M. Then set up another server to handle only last names that begin with N-Z, thus splitting the load between the two servers.

In addition to changing the pollQuery, you also need to change the lockName property of the ScheduledOrderService component so that it is unique for each server. Otherwise, if all the services use the same lockName, only one instance will run at a time.

 
loading table of contents...