Skip to content


Drools configuration with Spring. Invoke processes over AOP (Part 3)

Example.

	@Transactional
	public void transfer(Account from, Account to, Double amount, final Boolean isSendNotification) throws Exception
	{
		couldDebit(from, amount);
		from.setAmmount(from.getAmmount() - amount);
		to.setAmmount(to.getAmmount() + amount);
		from.save();
		to.save();
		if( isSendNotification )
			emailQueue.sendMessage(createTransferNotification(from, to));
	}

We have a method ‘transfer’ in Account service transferd money from one customer account to another acount of same customer.
First, we validate that from account has enough amount of money.
Second, we transfer money.
And at the end we send notification using asyncronus queue email service.

Flow:


When we start using drools flow validation, we moved validation in rules.
Now our universal validation for debit account looks next:

	rule AccountAmountEnoughToDebit
	ruleflow-group "validateAccount"
	when
		$account: Account( amount > 0 )
		$debitAmount: processInstance : WorkflowProcessInstance( eval( ((Integer)processInstance.getVariable("debitAmount"))))
	then
		if( $account.getAmount() <  $debitAmount )
			insert( error(kcontext,"Insufficient amount of money on account "+$account.getAccountNumber(),$account));
	end

Now our method looks next:

	@WorkProcess(value = "com.company.workflow.process.ProcessTransferWorkflowProcess", flowParameterNames = { "from", "to","amount", "isSendNotification" }, wrappedWorkItemName = "TransferWorkItemHandler")
	@Transactional
	public void transfer(Account from, Account to, Double amount, final Boolean isSendNotification) throws Exception
	{
		from.setAmount(from.getAmount() - amount);
		to.setAmount(to.getAmount() + amount);
		from.save();
		to.save();
	}

When transfer method invokes, WorkProcessFactory, that has setup to create proxy arround this method, will
load process with name ‘ProcessTransferWorkflowProcess’ and execute it.

What happens with notification (see next post)

Posted in development.

Tagged with , , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

You must be logged in to post a comment.