开发前准备
- 在本机配置好快嘉框架,参考快嘉框架v1.3更新及使用手册
- 开发工具及安装规划,参考开发工具及安装规划
将快嘉demo导入eclipse,调试demo-Rest和demo-batch
eclipse导入快嘉框架代码
选择existing maven projects
选择快嘉框架源码目录,不要导入base和bundle模块
导入eclipse后,项目视图大概如下所示,maven工程需要时间较长,请等待
maven工程全部导入后,整个eclipse视图大概如下所示
有个插件用法的错误提示,不用管它,点“resovled all later” 按钮,弹出界面如下,点“OK”按钮关闭掉。
eclipse调试demo-Rest
如果没啥问题,正常显示如下
有可能会出如下问题
这个是eclipse工作空间下的demo-Rest部署目录(一般是${eclipseWorkspace}.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\demo-Rest\WEB-INF)没有同步更新web.xml导致,我理解是eclipse的maven插件对profile的支持还没有很完善,按照以下方法处理就可以了,处理完后能持续用好长一段时间,这个问题在调试的过程中可能还会出现,出现一次处理一次就好了。
源码目录demo/Rest/Main下执行“mvn clean package -Pnocheck,local”后,target目录下会生成demo-Rest目录,将demo-Rest\WEB-INF\web.xml替换掉${eclipseWorkspace}.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\demo-Rest\WEB-INF\web.xml后,重启eclipse中的tomcat即可。
eclipse调试demo-batch
在demo-batch工程里打开com.fastjrun.demo.BatchInit类,该参数提供了main函数
配置运行参数
点“Run”按钮
确保两个任务正常执行
用法:接口变化
#### 在demo_user.xml中新增退出接口定义,在name为userServiceRest的节点下新增名为logout的method节点
2、logout的method节点定义如下
<method name="logout" version="" path="logout" remark="登出">
<request>
<object name="head" new="false"
class="com.fastjrun.packet.BaseRestRequestHead"></object>
<object name="body" new="true"
class="packet.user.LogoutRestRequestBody">
<field name="uuid" dataType="java.lang.String" length="64"
canBeNull="false" remark="登录凭证" />
</object>
</request>
<response>
<object name="head" new="false"
class="com.fastjrun.packet.BaseRestResponseHead"></object>
</response>
</method>
3、在快嘉源码目录下执行如下命令,重新生成Bundle代码,并发布demo-bundle.jar到本地仓库
cd demo
mvn clean install -Pnocheck -pl Bundle
4、在demo-service-impl工程上执行Maven->>update project…
5、执行完后,demo-service-impl工程下的UserServiceImpl报错,出错信息和解决方法如截图所示
6、我们在UserServiceImpl重载logout方法如下
@Override
public BaseRestResponse<BaseRestDefaultResponseBody> logout(
BaseRestRequest<LogoutRestRequestBody> request) {
String uuid=request.getBody().getUuid();
String deviceId = request.getHead().getDeviceId();
this.coreUserService.logOut(uuid, deviceId);
return RestResponseHelper.getSuccessResult();
}
7、做单元测试,单元测试类(src/test/java/com/fastjrun/demo/service/impl/UserServiceRestImplTeset.java)
先执行登录的单元测试用例testLogin
记录下成功后的uuid:1b633c6889ec451eb0f00d63c7244177 再执行登出的单元测试用例testlogout
@Test
public void testLogout() {
BaseRestRequest<LogoutRestRequestBody> request = new BaseRestRequest<LogoutRestRequestBody>();
BaseRestRequestHead head = new BaseRestRequestHead();
LogoutRestRequestBody body = new LogoutRestRequestBody();
body.setUuid("1b633c6889ec451eb0f00d63c7244177");
String deviceId = "servicetest";
head.setDeviceId(deviceId);
request.setHead(head);
request.setBody(body);
BaseRestResponse<BaseRestDefaultResponseBody> result = userServiceRest.logout(request);
JSONObject jsonObject = JSONObject.fromObject(result);
log.info(jsonObject);
}
执行结果如下
8、单元测试完成后,参考快嘉框架v1.3使用手册重新生成demo-Rest.war,新的war中提供了这次将提供了退出接口的服务,这里笔者不再赘述如何用postman来测试验证,请读者自行实践。
至此,一个接口如何增加的例子就讲完了,这个接口确实复用了自动登录接口中用到的核心服务CoreUserService提供的logout方法,所以实现起来很快;但即使没有这个可复用的方法,重头写起工作量应该也不会很大,总之,接口新增、修改、版本升级我们都可以在这个demo_user.xml文件进行,然后再去调整因为调整这个文件后导致的实现类的变化即可。
用法二:表结构变化
这里需要维护的是pdm文件,pdm文件变化后,在快嘉源码目录下执行如下命令,将会重新生成Base代码,并发布demo-base.jar到本地仓库
cd demo
mvn clean install -Pnocheck -pl Base
Base模块中的代码都是对应于数据库单表的实体bean和基于该实体bean的增删改查的基本操作类,这里贴一份BaseUserDaoTest.java代码,大家可以参考这份代码研究一下Base中实体bean的基本操作类的相关用法
public class BaseUserDaoTest extends BaseSpringTestNGTest {
@Autowired
BaseUserDao baseUserDao;
@Test
public void testInsert() {
User user = new User();
String curTime = TimeHelper.getCurrentTime(TimeHelper.DF17);
Timestamp curDate = new Timestamp(System.currentTimeMillis());
user.setCreateTime(curDate);
user.setEmail("sa4@fastjrun.org");
user.setLastLoginTime(curTime);
user.setLastModifyTime(curDate);
user.setLoginErrCount(0);
user.setLoginName("sa3");
user.setLoginPwd("1234566777");
user.setMobileNo("03211111112");
user.setNickName("sa3");
user.setSex(2);
user.setStatus("1");
baseUserDao.insert(user);
}
@Test
public void testSelectById() {
User user = baseUserDao.selectById(38);
log.info(user);
}
@Test
public void testUpdateById() {
User user = baseUserDao.selectById(38);
log.info(user);
Timestamp curDate = new Timestamp(System.currentTimeMillis());
user.setCreateTime(curDate);
user.setLastModifyTime(curDate);
baseUserDao.updateById(user);
User user2 = baseUserDao.selectById(38);
log.info(user2);
}
@Test
public void testDeleteById() {
User user = baseUserDao.selectById(38);
log.info(user);
int res = baseUserDao.deleteById(1);
log.info(res);
User user2 = baseUserDao.selectById(1);
log.info(user2);
}
@Test
public void testTotalCount() {
int res = baseUserDao.totalCount();
log.info(res);
}
@Test
public void testQueryForList() {
List<User> list = baseUserDao.queryForList();
if (list != null) {
Iterator<User> itera = list.iterator();
while (itera.hasNext()) {
log.info(itera.next());
}
}
}
@Test
public void testQueryForLimitList() {
RowBounds rowBounds = new RowBounds(2, 4);
List<User> list = baseUserDao.queryForLimitList(rowBounds);
if (list != null) {
Iterator<User> itera = list.iterator();
while (itera.hasNext()) {
log.info(itera.next());
}
}
}
@Test
public void testTotalCountCondition() {
int res = baseUserDao.totalCountCondition(" and sex =1");
log.info(res);
}
@Test
public void testQueryForListCondition() {
List<User> list = baseUserDao.queryForListCondition(" and sex =0");
if (list != null) {
Iterator<User> itera = list.iterator();
while (itera.hasNext()) {
log.info(itera.next());
}
}
}
@Test
public void testQueryForLimitListCondition() {
RowBounds rowBounds = new RowBounds(2, 4);
List<User> list = baseUserDao.queryForLimitListCondition(" and sex =1",rowBounds);
if (list != null) {
Iterator<User> itera = list.iterator();
while (itera.hasNext()) {
log.info(itera.next());
}
}
list = baseUserDao.queryForLimitListCondition(" and sex =0",rowBounds);
if (list != null) {
Iterator<User> itera = list.iterator();
while (itera.hasNext()) {
log.info(itera.next());
}
}
}
@Test
public void testQueryForListConditionMul() {
final RowBounds rowBounds = new RowBounds(2, 4);
final Semaphore semaphore = new Semaphore(3);
ExecutorService executorService = Executors.newCachedThreadPool();
List<Future<Boolean>> resultList = new ArrayList<Future<Boolean>>();
for (int i = 0; i < 1000; i++) {
try {
semaphore.acquire();
} catch (InterruptedException e1) {
log.warn("can't acquire semaphore", e1);
continue;
}
Callable<Boolean> run = new Callable<Boolean>() {
public Boolean call() {
try {
testCondition(rowBounds);
} catch (Exception e) {
log.warn("BatchInsert Task for", e);
} finally {
// 访问完后,释放
semaphore.release();
}
return true;
}
};
Future<Boolean> future = executorService.submit(run);
resultList.add(future);
}
Boolean isFinished = false;
while (!isFinished) {
for (Future<Boolean> fs : resultList) {
try {
isFinished = !isFinished && fs.get();
log.debug(isFinished);
if (!isFinished) {
resultList.remove(fs);
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
}
private void testCondition(RowBounds rowBounds) {
List<User> list = baseUserDao.queryForLimitListCondition(" and sex =0",rowBounds);
if (list != null) {
Iterator<User> itera = list.iterator();
while (itera.hasNext()) {
log.info(itera.next());
}
}
}
}