CampaignService
CampaignService
The CampaignService
is responsible for managing campaigns — including manual launches and status transitions.
It acts as a facade for initiating campaign execution, including the creation of CampaignExecution
.
🔧 Method: startCampaign(Long campaignId)
startCampaign(Long campaignId)
@Service
public class CampaignService {
@Autowired
private CampaignRepository campaignRepository;
@Autowired
private TransactionTemplateBuilder ttb;
@Autowired
private CampaignExecutionService campaignExecutionService;
@Transactional(rollbackFor = Throwable.class)
public void startCampaign(Long campaignId) {
Campaign campaign = campaignRepository.getSync(campaignId);
Assert.isTrue(campaign.isActive(), "Campaign is not active");
if (campaign.getExecutionType() == CampaignExecutionType.AUTOMATIC) {
if (campaign.getAutomaticCampaignStatus() != AutomaticCampaignStatus.SCHEDULED) {
campaign.setAutomaticCampaignStatus(AutomaticCampaignStatus.SCHEDULED);
}
} else if (campaign.isNotInExecution()) {
CampaignExecution execution = campaignExecutionService.createExecution(campaign);
TransactionUtils.afterTransaction(() -> {
ttb.requiresNew().executeWithoutResult(status ->
campaignExecutionService.runExecution(execution.getId())
);
});
}
}
}
📌 Behavior
Retrieves the
Campaign
usinggetSync(...)
— ensures the entity is up-to-date and synchronizedVerifies that the campaign is active
If the campaign is automatic, it sets the status to
SCHEDULED
(if not already)If it's a manual campaign and no execution is currently
IN_PROGRESS
, it:Creates a new
CampaignExecution
After the current transaction commits (
TransactionUtils.afterTransaction
), it launches the execution in a new transaction
🔒 Transactional Handling
The entire method is wrapped with @Transactional(rollbackFor = Throwable.class)
and uses TransactionTemplateBuilder
to ensure safe execution in an isolated transaction.
Last updated
Was this helpful?