📄 workflowsession.java
字号:
package cn.com.iaspec.workflow.vo.workflow;
import java.util.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import cn.com.iaspec.workflow.exception.*;
import cn.com.iaspec.workflow.util.*;
import com.sunyard.sunflow.client.*;
public class WorkflowSession
implements java.io.Serializable,HttpSessionBindingListener{
private String hostName;
private int portName;
private String sessionId;
private UserInfo userinfo=null;
private static long connectionCount=0;
private static long freeConnCount=0;
private static SunflowClient sunflowClient=null;
;
private static HashMap sunflowclientHash=new HashMap();
private static Logger logger=Logger.getLogger(WorkflowSession.class);
public void setHostName(String hostName){
this.hostName=hostName;
}
public void setPortName(int portName){
this.portName=portName;
}
public void setSessionId(String sessionId){
this.sessionId=sessionId;
}
public String getHostName(){
return hostName;
}
public int getPortName(){
return portName;
}
public String getSessionId(){
return sessionId;
}
public void setSunflowClient(SunflowClient sunflowClient){
this.sunflowClient=sunflowClient;
}
public void setSunflowclientHash(HashMap sunflowclientHash){
this.sunflowclientHash=sunflowclientHash;
}
public SunflowClient getSunflowClient(){
return sunflowClient;
}
public HashMap getSunflowclientHash(){
return sunflowclientHash;
}
public WorkflowSession(UserInfo userinfo,SunflowClient sunflow){
this.userinfo=userinfo;
this.sunflowClient=sunflow;
}
public WorkflowSession(){
}
/**
* 在HashMap查询判断UserInfo对象是否存在,如果存在就把对应的sunflowclient对象取出来
* @param userinfo UserInfo
* @return boolean
*/
public static boolean checkSunflowClient(UserInfo userinfo){
boolean issunflowclient=false;
Set entry=sunflowclientHash.entrySet();
//printElements(entry);
Iterator it=entry.iterator();
while(it.hasNext()){
Map.Entry me=(Map.Entry)it.next();
if(me.getKey().equals(userinfo.getLoginName())){
System.out.println(me.getKey()+"---:=----"+me.getValue());
issunflowclient=true;
sunflowClient=(SunflowClient)me.getValue();
}
}
return issunflowclient;
}
/**
*在HashMap中 删除key值为userinfo对象对应的sunflowclient对象,并同时关闭工作流连接
* @param userinfo UserInfo
* @return boolean
*/
public static boolean deleteSunflowClient(UserInfo userinfo){
WorkflowSessionManager.getInstance().freeWorkflowClient(userinfo);
return true;
/* boolean flag=false;
if(checkSunflowClient(userinfo))
{
sunflowclientHash.remove(userinfo.getLoginName());
logger.info("-----删除userinfo成功-----");
Reset(sunflowClient);
flag= true;
}else
{
flag= true;
}
return flag;*/
}
/**
* 将session断开连接,从线程池中删除
* @param userInfo UserInfo
*/
public static synchronized void logoutWorkflowClient(UserInfo userInfo){
WorkflowSessionManager.getInstance().logoutWorkflowClient(userInfo);
}
/**
*
* @param userinfo UserInfo
* @param sunflowclient SunflowClient
* @return boolean
*/
public static boolean addSunflowClient(UserInfo userinfo,
SunflowClient flowclient){
if(!checkSunflowClient(userinfo)){
sunflowclientHash.put(userinfo.getLoginName(),flowclient);
logger.info("-----添加userinfo成功-----");
return true;
}
return false;
}
/**
* 连接工作流客户端(返回sunflowClient实例)
* @param userinfo UserInfo
* @return SunflowClient
*/
public static SunflowClient getSunflowClient(UserInfo userinfo){
try{
return WorkflowSessionManager.getInstance().getWorkflowClient(userinfo);
}
catch(Exception ex){
ex.printStackTrace();
}
return null;
/*SunflowClient workflowClient=null;
try
{
if(checkSunflowClient(userinfo))
{
if(sunflowClient!=null)
{
return sunflowClient;
}else
{
workflowClient =connSunflowClient(userinfo);
}
}else
{
workflowClient =connSunflowClient(userinfo);
}
}catch(Exception e)
{
e.printStackTrace();
logger.info("-----连接工作流客户端----失败-----");
}
connectionCount++;
logger.info("workflow connectionCount is:"+connectionCount);
return workflowClient;*/
}
/**
* 创建工作流连接,返回SunflowClient工作流连接对象
* @param userinfo UserInfo
* @return SunflowClient
*/
public static SunflowClient connSunflowClient(UserInfo userinfo)
throws CannotLinkToSunflowServerException{
long startTime=System.currentTimeMillis();
SunflowClient flowClient=null;
try{
flowClient=WorkflowUtil.getSunflowClient();
flowClient.connect(userinfo.getLoginName(),userinfo.getPassword());
logger.info("-----连接工作流客户端----成功-----");
addSunflowClient(userinfo,flowClient);
}
catch(Exception e){
e.printStackTrace();
throw new CannotLinkToSunflowServerException("不能连接到工作流引擎。");
}
logger.info("Execution "+userinfo.getLoginName()+
"'s connSunflowClient time: "+(System.currentTimeMillis()-startTime)+
" ms.");
return flowClient;
}
/**
* 断开与工作流的连接
* @param workflowClient SunflowClient
*/
public static void Reset(SunflowClient workClient){
if(workClient!=null){
try{
workClient.disconnect(); //操作完成断开连接
logger.info("-----断开工作流客户端连接----成功-----");
}
catch(Exception e){
e.printStackTrace();
logger.info("-----断开工作流客户端连接----失败 -----");
}
finally{
workClient=null;
}
freeConnCount++;
logger.info("workflow freeConnCount is:"+freeConnCount);
}
}
/**
*
* @param e HttpSessionBindingEvent
*/
public void valueBound(HttpSessionBindingEvent e){
}
/**
*
* @param e HttpSessionBindingEvent
*/
public void valueUnbound(HttpSessionBindingEvent e){
}
/**
* testPrint
* @param c Collection
*/
public static void printElements(Collection c){
Iterator it=c.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
/*
* testPrint
*/
public void printlnclient(){
Set keys=sunflowclientHash.keySet();
System.out.print("Key:");
//printElements(eys );
printElements(keys);
java.util.Collection values=sunflowclientHash.values();
System.out.print("Value:");
printElements(values);
}
public static void main(String[] args){
WorkflowSession workflowsession=new WorkflowSession();
WorkflowUtil.hostName="10.21.109.112";
WorkflowUtil.portName=1093;
UserInfo userinfo1=new UserInfo();
userinfo1.setUserId("10001");
userinfo1.setLoginName("admin");
userinfo1.setPassword("123");
UserInfo userinfo2=new UserInfo();
userinfo2.setUserId("10014");
userinfo2.setLoginName("wangxl");
userinfo2.setPassword("123");
UserInfo userinfo3=new UserInfo();
userinfo3.setUserId("10012");
userinfo3.setLoginName("liyq");
userinfo3.setPassword("123");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -