EJB3 0 方法快取

2021-08-31 06:39:54 字數 3107 閱讀 1919

公司維護的系統,涉及好多許可權管理,區域資訊,公共配置資訊,之前系統都是通過快取去讀取這些配置資訊,現在為了滿足定製,開發了各種管理功能,這樣就導致之前從快取中讀取的資訊無法取到最新。

於是考慮用ehcache來實現方法快取,這個在spring 中整合是非常簡單的,這邊考慮在ejb3中整合ehcache。

廢話不說,直接**,demo 如下:

定義註解

@target(elementtype.method)

@retention(retentionpolicy.runtime)

public @inte***ce cache

定義註解取值

public enum cacheflag

快取處理例項

/**

* ehcache handler

* * @author

* @version [版本號, 2011-3-13]

* @see [相關類/方法]

* @since [產品/模組版本]

*/public class cachehandler

private void initcache()

}public void checkcachestatus(method method)

}/**

* 初始化快取

* @param method

* method

* @return void [返回型別說明]

* @exception throws [違例型別] [違例說明]

* @see [類、類#方法、類#成員]

*/private void initcacheforclass(method method)

methodinfo = med.getname() + ":" + arrays.tostring(med.getparametertypes());

methodcache.add(methodinfo);

}else if (cacheannotation.value().equals(cacheflag.delete))

methodinfo = med.getname() + ":" + arrays.tostring(med.getparametertypes());

methodcache.add(methodinfo);}}

}}

/*** 獲取 cache key

* @param classname

* @param methodname

* @param paramarrayofobject

* @return [引數說明]

* * @return string [返回型別說明]

* @exception throws [違例型別] [違例說明]

* @see [類、類#方法、類#成員]

*/protected string getcachekey(string classname, string methodname,

object paramarrayofobject)

else}}

return cachekey.tostring();

}@suppresswarnings("unchecked")

private string getargumentstring(object paramobject)

if (localclass.isassignablefrom(boolean.class))

return arrays.tostring((object) (object) paramobject);

}return paramobject.tostring();

}public boolean iscache(invocationcontext invocationcontext)

}return false;

}public boolean isclear(invocationcontext invocationcontext)

}return false;

}public void clearcache()

public static cachehandler getinstance()}}

return cachehandler;

}public boolean isenable()

public synchronized void destroy()

public object getcacheelement(string cachekey) throws clonenotsupportedexception

return null;

}public void addresult(string cachekey, object cachevalue)

}

ejb ***定義

public class cacheinterceptor

// 清空快取

if (isclear)

if (iscache)

}catch (exception e)

return result;}}

else

return invocationcontext.proceed();

}}

ejb 方法使用

@override

@interceptors(cacheinterceptor.class)

@cache(cacheflag.add)

public cabindomain querycabininfo(cabinpk cabinpk)

@override

@interceptors(cacheinterceptor.class)

@cache(cacheflag.delete)

public void addcabininfo(cabindomain cabindomain)

其他:部署在jboss上

簡單的demo,大家覺的,有哪些地方需要改進的?

EJB3 0 註解列表

color red 會話bean與訊息驅動bean。color 1,會話bean。有狀態bean sataeful 無狀態bean sataeless 業務方法是有狀態會話bean的刪除方法 remove 使poji成為會話bean遠端業務介面 remote 使poji成為會話bean本地業務介面 ...

EJB3 0學習筆記

一.ejb3.0分為會話bean session bean mdb message driver bean 訊息驅動bean,實體bean entity bean 1.session bean 又分為有狀態的 stateful 和無狀態的 stateless stateful session bea...

EJB3 0呼叫儲存過程

要呼叫儲存過程,我們可以通過 entitymanager 物件的 createnativequery 方法執行 sql 語句 注意 這裡說的是sql 語句,不是 ejb3 ql 呼叫儲存過程的 sql 格式如下 在 ejb3 中你可以呼叫的儲存過程有兩種 1 無返回值的儲存過程。2 返回值為 res...