Call control for sip call in asterisk

I am new in asterisk. There is a solution named A2Billing for asterisk but i want to build something like below:

“Caller A is a registered sip peer in asterisk. A called to any number. After establish the call need to connect MySQL database to check A’s talk time balance after each 10 second and deduct talk time for 10 second. When the talk time balance comes to 0 forcefully drop the call”

How can do this ? I am good in java. can java can handle this or what ? any suggestion welcome.

You can do this using dialplan instructions, if you want to do it external dialplan use an AGI using your preferred language PHP has a helpful supported class called phpagi.