⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 updatelosedaotest.java

📁 精通EJB3源码
💻 JAVA
字号:
package junit.test;

import java.util.Properties;
import java.util.Timer;

import javax.naming.Context;
import javax.naming.InitialContext;

import org.junit.BeforeClass;
import org.junit.Test;

import com.foshanshop.ejb3.UpdateLoseDAO;
import com.foshanshop.ejb3.bean.User;

public class UpdateLoseDAOTest {
	private static UpdateLoseDAO dao;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		 try {
			Properties props = new Properties();
			 props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
			 props.setProperty(Context.PROVIDER_URL, "localhost:1099");
			 props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
			 InitialContext ctx = new InitialContext(props);
			 dao = (UpdateLoseDAO) ctx.lookup("UpdateLoseDAOBean/remote");
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testUpdateLose() {
		dao.init(new User(10000));
		//下面使用两个定时器引起事务并发
		Timer timer = new Timer();
		//为了符合时间片分配表的执行顺序,让payElectricityFee()方法先执行
		timer.schedule(new ATask(), 1);
		
		Timer timer2 = new Timer();
		timer2.schedule(new BTask(), 1000);
		try {
			Thread.sleep(15000);
		} catch (InterruptedException e) {}
		timer.cancel();
		timer2.cancel();
	}

	@Test
	public void testSecondUpdateLose() {
		dao.init(new User(10000));
		//下面使用两个定时器引起事务并发
		Timer timer = new Timer();
		//为了符合时间片分配表的执行顺序,让secondPayElectricityFee()方法先执行
		timer.schedule(new CTask(), 1);
		
		Timer timer2 = new Timer();
		timer2.schedule(new DTask(), 1000);
		try {
			Thread.sleep(15000);
		} catch (InterruptedException e) {}
		timer.cancel();
		timer2.cancel();
	}

	static class ATask extends java.util.TimerTask{
		@Override
		public void run() {
			System.out.println("========交电费方法开始=========");
			System.out.println(dao.payElectricityFee(1));
			System.out.println("========交电费方法结束=========");
		}
	}
	
	static class BTask extends java.util.TimerTask{
		@Override
		public void run() {
			System.out.println("========存工资方法开始=========");
			try {
				System.out.println(dao.saveLaborage(1));
			} catch (RuntimeException e) {
				System.out.println("存工资方法出错");
			}
			System.out.println("========存工资方法结束=========");
		}
	}
	
	static class CTask extends java.util.TimerTask{
		@Override
		public void run() {
			System.out.println("========交电费方法开始=========");
			System.out.println(dao.secondPayElectricityFee(1));
			System.out.println("========交电费方法结束=========");
		}
	}
	
	static class DTask extends java.util.TimerTask{
		@Override
		public void run() {
			System.out.println("========存工资方法开始=========");
			System.out.println(dao.secondSaveLaborage(1));
			System.out.println("========存工资方法结束=========");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -