前言:常年离线在线开发实施Windchill9.0和Windchill10.0。
带人经验极其丰富,可帮公司创建Windchill开发团队。
高效,务实,认真,负责!
所有文章全部原创,均经过测试,如有不对,欢迎留言指正。
本文不说这东西从哪里来,要到那里去,只说如何去应用。
可以通过计划任务队列去有规律有间隔的执行一些任务,和定时器类似。
请按照以下步骤进行:
第一步,编写创建队列对象的代码,详细说明参见代码:
编写两个类com.queue.CreateQueue 和 com.queue.TestQueue,CreateQueue 里编写创建队列的代码,TestQueue里编写计划任务队列执行时所调用的类和方法。
CreateQueue.java文件如下:
package com.queue;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Enumeration;
import wt.org.OrganizationServicesHelper;
import wt.org.WTPrincipalReference;
import wt.org.WTUser;
import wt.scheduler.ScheduleItem;
import wt.scheduler.SchedulingHelper;
import wt.util.WTException;
/**
* <p>Description: </p>
* @author:
* @time: Oct 29, 2010 10:56:35 PM
* @version 1.0
*/
public class CreateQueue {
public static void main(String args[]) throws WTException{
createScheduleQueue();
}
/**
* 创建计划执行队列,只需要执行一次即可,以后当windchill服务启动则按照设定的时间定时执行任务
* 11:25:38 PM
* @throws WTException
*/
public static void createScheduleQueue() throws WTException{
System.out.println("开始创建计划执行队列... ...");
//创建计划队列对象
ScheduleItem si = ScheduleItem.newScheduleItem();
//设置该队列的描述信息
si.setItemDescription("测试计划执行队列");
//设置队列的名称,一般以英文命名
si.setQueueName("TestScheduleItem");
//设置要执行的类和方法
//类名
si.setTargetClass(TestQueue.class.getName());
//方法名
si.setTargetMethod("testQueue");
si.setToBeRun(-1l);
//设置开始时间,在这里我设置为队列创建后立即执行
Date today = new Date();
Timestamp timestamp = new Timestamp(today.getYear(), today.getMonth(),today.getDate(),
00, 00, 0, 0); //这四个参数依次为小时,分,秒,毫秒
si.setStartDate(timestamp);
//设置执行周期,这里设置为每隔10秒执行一次
si.setPeriodicity(10);
//设置执行该任务的用户,如果是操作Windchill对象的话会牵扯到权限控制,这里设置为管理员执行
WTUser administrator = getUserFromName("Administrator");
WTPrincipalReference p = null;
si.setPrincipalRef(WTPrincipalReference.newWTPrincipalReference(administrator));
//最后,将计划任务对象添加到队列
si = SchedulingHelper.service.addItem(si, null);
System.out.println("计划执行队列创建完毕... ..." + "'启动时间为'" + si.getStartDate() + "',间隔为'" + si.getPeriodicity() + "'!"); }
/**
* 根据用户名得到用户
* @param name 用户名
* @throws WTException
* return WTUser
*/
public static WTUser getUserFromName(String name) throws WTException {
Enumeration enumUser = OrganizationServicesHelper.manager.findUser(WTUser.NAME, name);
WTUser user = null;
if (enumUser.hasMoreElements())
user = (WTUser) enumUser.nextElement();
if (user == null) {
enumUser = OrganizationServicesHelper.manager.findUser(WTUser.FULL_NAME, name);
if (enumUser.hasMoreElements())
user = (WTUser) enumUser.nextElement();
}
if (user == null) {
throw new WTException("系统中不存在用户名为'" + name + "'的用户!");
}
return user;
}
}
TestQueue .java文件如下:
package com.queue;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import wt.admin.AdministrativeDomainHelper;
import wt.method.MethodContext;
import wt.session.SessionAuthenticator;
/**
* <p>Description: </p>
* @author:
* @time: Oct 29, 2010 11:29:46 PM
* @version 1.0
*/
public class TestQueue {
/**
* 计划队列执行时调用的方法
* 11:49:00 PM
*/
public static void testQueue(){
TimeZone zone = TimeZone.getTimeZone("GMT+8:00");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
sdf.setTimeZone(zone);
String currentTime = sdf.format( new Date());
System.out.println("测试计划执行队列,当前时间:" + currentTime );
//在这里编写处理业务相关的代码,如果操作Windchill持久化对象时出现"找不到活动方法的上下文"错误时,可增加以下代码进行处理
MethodContext mc = MethodContext.getContext(Thread.currentThread());
if (mc == null)
mc = new MethodContext(null, null);
if (mc.getAuthentication() == null) {
SessionAuthenticator sa = new SessionAuthenticator();
mc.setAuthentication(sa.setUserName(AdministrativeDomainHelper.ADMINISTRATOR_NAME));
}
}
}
第二步,启动windchill服务,shell中执行以下命令运行CreateQueue的main方法
java com.queue.CreateQueue
弹出授权请求,输入管理员的用户名和密码,因为其他用户是没有权限创建队列的。
观察shell和methodServer中的输出信息,发现并未出错,同时,正在以10秒的间隔时间执行TestQueue类里的TestQueue()方法。
打开站点-实用程序-队列管理器,发现已存在名为TestScheduleItem的队列。
shell中的输出信息
methodServer中的输出信息
队列管理器
至此,计划执行任务队列创建完毕。
PS:
本文只是简单的描述了如何去创建一个队列对象。如果想写的更全面些,以下代码可能会有用处:
1,通过ScheduleItem对象的ITEM_NAME和QUEUE_NAME查询一个ScheduleItem对象
/**
* 通过ScheduleItem对象的ITEM_NAME和QUEUE_NAME查询一个ScheduleItem对象
* 12:25:41 AM
* @param schedule_item_name
* @param Schedule_queque_name
* @return
* @throws WTException
*/
public static ScheduleItem getScheduleItemFabu(String schedule_item_name,String Schedule_queque_name) throws WTException {
ScheduleItem scheduleItem = null;
QuerySpec queryspec = new QuerySpec(ScheduleItem.class);
queryspec.appendWhere(new SearchCondition(ScheduleItem.class, ScheduleItem.ITEM_NAME, SearchCondition.EQUAL,
schedule_item_name));
queryspec.appendAnd();
queryspec.appendWhere(new SearchCondition(ScheduleItem.class, ScheduleItem.QUEUE_NAME, SearchCondition.EQUAL,
Schedule_queque_name));
queryspec.appendOrderBy(ScheduleItem.class, ScheduleItem.START_DATE, true);
QueryResult queryResult = PersistenceHelper.manager.find(queryspec);
while (queryResult.hasMoreElements()) {
scheduleItem = (ScheduleItem) queryResult.nextElement();
}
return scheduleItem;
}
2,修改已存在的队列对象的相关属性
在获取ScheduleItem对象后,可修改其相关属性,下面是一些示例:
ScheduleItem si= getScheduleItemFabu("a","b");
si.setStartDate(timestamp);
si.setItemDescription("修改队列描述");
si.setPeriodicity(60);
si.setToBeRun(-1l);
//保存修改后的队列对象
si=SchedulingHelper.service.modifyItem(si);
常年离线在线开发实施Windchill9.0和Windchill10.0。
带人经验极其丰富,可帮公司创建Windchill开发团队。
高效,务实,认真,负责!
分享到:
相关推荐
NULL 博文链接:https://windchill9.iteye.com/blog/670301
NULL 博文链接:https://windchill9.iteye.com/blog/1604732
windchill工作流,主要是了解如何配置工作流,并且在系统中怎么跑;以及对业务的理解及在windchill10.0系统页面操作之类的
Windchill工作流数据库字典:工作流模板、工作流实例、工作流任务、工作流表达式、工作流表达式模板
导出命令需要在windchill shell中执行: windchill wt.load.LoadFromFile -d loadFiles/ext2/ExportType.xml -u wcadmin -p wcadmin
windchill资料windchill资料windchill资料windchill资料
windchill客制化开发很好的资源,适用于windchill 10.0 的开发
1.在windchill shell中输入windchill -version,用来查看windchill的版本 2. xconfmanager -s name=value -t 目标文件 -p 用来在xconf和properties文件中增加条目,例如xconfmanager -s wt.servlet.ajp.minPort=...
windchill基础操作.rar windchill基础操作 工作流管理 业务管理: n 了解定义业务环境中所涉及的进程 n 确定Windchill 组织的使用 n 创建组织 n 创建并管理用户帐户和组 n 确定管理组织、组和用户帐户的最佳做法 n ...
Windchill 10.0 开发建模文档
windchill学习windchill学习windchill学习windchill学习windchill学习windchill学习windchill学习windchill学习windchill学习windchill学习
windchill12.1.0.0 安装
windchill系统调优文档windchill
具体的内容有1、创建产品2、创建文件夹3、创建文档4、创建用户账号5、创建组6、创建更改请求7、创建部件新视图版本 8、创建可重用属性和全局枚举9、在组织内分配上下文创建者10、创建更改通告模板11、创建更改通告12...
windchill WTPart WTpartMaster 零件操作最全的通用方法
windchill项目管理指南,包括计划 团队 任务 资源等
windchill 远程调试详细介绍 windchill开发环境搭建 windchill服务端调试模式设置 windchill eclipse开发环境搭建详细介绍
WINDCHILL项目管理模块使用教程 WINDCHILL项目管理模块使用教程
windchill JCA介绍
windchill 中文客制化