You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Hybris中向Cron Job传递参数?

How to Pass Parameters to a Cron Job in Hybris

Passing custom parameters to a Hybris Cron Job involves three straightforward steps: defining the parameters in your job model, accessing them in your Performable class, and configuring them via the admin interface. Let’s walk through each step using your existing UserRetrievePerformable code as a foundation.


1. Add Custom Parameters to Your Job Model

First, extend your UserRetrieveJobModel (defined in your extension’s items.xml) with the parameters you need. For example, let’s add a maxUserCount integer (to limit how many users are retrieved) and a targetUserGroup string (to filter users by group):

<itemtype code="UserRetrieveJobModel" extends="JobModel" autocreate="true" generate="true">
    <attributes>
        <attribute qualifier="maxUserCount" type="java.lang.Integer">
            <modifiers read="true" write="true" search="true" optional="false"/>
            <defaultvalue>100</defaultvalue>
        </attribute>
        <attribute qualifier="targetUserGroup" type="java.lang.String">
            <modifiers read="true" write="true" search="true" optional="true"/>
        </attribute>
    </attributes>
</itemtype>

After adding these attributes, run ant clean all to generate the updated model and service classes.


2. Access Parameters in Your Performable Class

Now update your UserRetrievePerformable class to read these parameters from the UserRetrieveJobModel in the perform method. Here’s how your completed code would look:

public class UserRetrievePerformable extends AbstractJobPerformable<UserRetrieveJobModel> { 
    private static final Logger LOG = Logger.getLogger(UserRetrievePerformable.class); 
    
    @Autowired 
    private TotalCustomerFacade totalCustomerFacade; 

    @Override 
    public PerformResult perform(UserRetrieveJobModel userRetrieveJobModel) { 
        LOG.info("**********************************"); 
        LOG.info("Greeting from MyJobPerformable!!!"); 
        
        // Fetch the custom parameters from the job model
        Integer maxUsers = userRetrieveJobModel.getMaxUserCount();
        String targetGroup = userRetrieveJobModel.getTargetUserGroup();
        
        LOG.info(String.format("Max users to retrieve: %d", maxUsers));
        if (targetGroup != null) {
            LOG.info(String.format("Target user group: %s", targetGroup));
        }
        
        // Use the parameters in your business logic (example)
        // totalCustomerFacade.retrieveFilteredUsers(maxUsers, targetGroup);
        
        LOG.info("**********************************"); 
        return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED); 
    } 
}

Don’t forget to handle optional parameters (like targetUserGroup) with null checks to avoid runtime errors.


3. Configure Parameters via Backoffice

Once your code is deployed, set the parameters when creating or editing your Cron Job:

  • Navigate to System > Cron Jobs in Backoffice.
  • Create a new Cron Job or edit an existing one linked to your UserRetrieveJobModel.
  • In the job’s details panel, you’ll see your custom parameters (maxUserCount and targetUserGroup) as editable fields.
  • Enter your desired values, save the Cron Job, and when it runs, your perform method will use those values.

Quick Tips

  • For complex parameters (like references to Hybris items), use item types (e.g., UserGroupModel instead of String) in your items.xml.
  • Mark parameters as optional="true" if they aren’t required, and always validate input in your code.
  • Remember to rebuild your extension after modifying items.xml to ensure changes take effect.

内容的提问来源于stack exchange,提问作者vegankid22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:26:32