package com.atomikos.tomcat.jms;

import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;

import org.springframework.beans.factory.annotation.Autowired;

import com.atomikos.tomcat.demo.AccountManager;

public class MessageDrivenBank implements MessageListener {
	private AccountManager accountManager;

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public void onMessage(Message msg) {
		try {
			MapMessage m = (MapMessage) msg;
			int account = m.getIntProperty("account");
			int amount = m.getIntProperty("amount");
			accountManager.withdraw(account, amount);
			System.out.println("Withdraw of " + amount + " from account " + account);
		} catch (Exception e) {
			e.printStackTrace();

			// force rollback of the transaction by throwing runtime
			throw new RuntimeException(e.getMessage());
		}
	}

}
