下面是我的Spring Boot主类,那里有@Scheduled
bean
@EnableScheduling
@EnableBatchProcessing
@SpringBootApplication(scanBasePackages = { "com.mypackage" })
public class MyMain {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public static void main(String[] args) throws Exception {
SpringApplication.run(MyMain.class, args);
}
@Scheduled(cron = "0 00 05 * * ?")
private void perform() throws Exception {
jobLauncher.run(job, new JobParameters());
}
}
我将从命令行接收参数,这些参数需要作为作业参数输入。与@Scheduled
带注释的方法不带任何参数一样,我该如何实现。
答案 0 :(得分:1)
您可以注入类型 Session session = new Session.Builder()
.setName("Name")
.setDescription("Data gathered from App")
.setActivity(FitnessActivities.WALKING)
.setStartTime(steps.getStartDate(), TimeUnit.MILLISECONDS)
.setEndTime(endTime, TimeUnit.MILLISECONDS)
.build();
DataSource stepsDataSource = new DataSource.Builder()
.setAppPackageName(context.getPackageName())
.setDataType(DataType.TYPE_STEP_COUNT_DELTA)
.setName("App - step count")
.setType(DataSource.TYPE_RAW)
.build();
DataSource distanceDataSource = new DataSource.Builder()
.setAppPackageName(context.getPackageName())
.setDataType(DataType.TYPE_DISTANCE_DELTA)
.setName("App - step count")
.setType(DataSource.TYPE_RAW)
.build();
DataSource caloriesDataSource = new DataSource.Builder()
.setAppPackageName(context.getPackageName())
.setDataType(DataType.TYPE_CALORIES_EXPENDED)
.setName("App - step count")
.setType(DataSource.TYPE_RAW)
.build();
// create data points
DataSet dSteps = DataSet.create(stepsDataSource);
DataPoint pSteps = dSteps.createDataPoint().setTimeInterval(steps.getStartDate(), endTime, TimeUnit.MILLISECONDS);
pSteps.getValue(Field.FIELD_STEPS).setInt(Math.round(steps.getMeasurement()));
dSteps.add(pSteps);
DataSet dDistance = DataSet.create(distanceDataSource);
DataPoint pDistance = dDistance.createDataPoint().setTimeInterval(steps.getStartDate(), endTime, TimeUnit.MILLISECONDS);
float distanceKm = UnitConvertUtil.convertKmFromMile(distance.getMeasurement());
pDistance.getValue(Field.FIELD_DISTANCE).setFloat(distanceKm);
dDistance.add(pDistance);
DataSet dCalories = DataSet.create(caloriesDataSource);
DataPoint pCalories = dCalories.createDataPoint().setTimeInterval(steps.getStartDate(), endTime, TimeUnit.MILLISECONDS);
pCalories.getValue(Field.FIELD_CALORIES).setFloat(98);
dCalories.add(pCalories);
SessionInsertRequest insertRequest = new SessionInsertRequest.Builder()
.setSession(session)
.addDataSet(dCalories)
.build();
Fitness.getSessionsClient(context, Objects.requireNonNull(
GoogleSignIn.getLastSignedInAccount(context))).insertSession(
insertRequest).addOnCompleteListener(
task -> {
if (task.isSuccessful()) {
Log.d(TAG, "SUCCESS ACTIVITY");
} else {
Log.d(TAG, "FAILURE ACTIVITY");
}
});
的bean并使用它获取应用程序参数:
'' OR Test=1
您可以在Accessing Application Arguments部分中找到有关ApplicationArguments
类型的更多详细信息。
希望这会有所帮助。