Java tutorial
/* * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.apress.prospringintegration.springenterprise.stocks.transactions.template; import com.apress.prospringintegration.springenterprise.stocks.dao.StockBrokerService; import com.apress.prospringintegration.springenterprise.stocks.dao.StockDao; import com.apress.prospringintegration.springenterprise.stocks.model.Stock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; import java.util.Calendar; import java.util.Random; @Component public class TransactionalStockBrokerService implements StockBrokerService { @Autowired @Qualifier("transactionTemplate") private TransactionTemplate transactionTemplate; @Autowired @Qualifier("jdbcTemplateStockDao") private StockDao stockDao; // inserts at least 25% duplicate inventory code. public void preFillStocks(final String exchangeId, final String... symbols) { final Random random = new Random(); transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { int i = 0; for (String sym : symbols) { float pp = (float) Math.random() * 100.0f; int qq = (int) Math.random() * 250; Stock s = new Stock(sym, "INV00" + i, exchangeId, pp, qq, Calendar.getInstance().getTime()); stockDao.insert(s); System.out.println("ORIG INVENTORY: " + s.getInventoryCode() + " "); int randomized = (random.nextInt(100) % 4) == 0 ? 0 : i; s.setInventoryCode("INV00" + randomized); System.out.println("NEW RANDOMIZED INVENTORY:" + s.getInventoryCode() + " " + randomized); stockDao.update(s); i++; } } }); } }