我有Jboss AS 6.1和war和sar應用程序。我用cassandra。所以我需要管理員訪問數據庫和緩存數據庫對象以便快速訪問。 所以我做了這樣的方式:從singletone遷移到java ee injection
public class AsaBlackSubsManager extends OperatorsCache<AsaBlackSubs> {
private final static Logger log = Logger.getLogger(AsaBlackSubsManager.class);
private final static String CF_NAME = "asa_black_subs";
private static AsaBlackSubsManager instance = null;
private MsisdnManager msisdnManager;
private AsaBlackSubsManager() {
reload();
msisdnManager = new MsisdnManager();
}
public static AsaBlackSubsManager getInstance() {
if (instance == null) {
instance = new AsaBlackSubsManager();
}
return instance;
}
public List<AsaBlackSubs> list() {
final List<AsaBlackSubs> list = new ArrayList<>();
final Statement query = QueryBuilder.select().from(CF_NAME);
final ResultSet rows = BasicManager.getInstance().getSession().execute(query);
for (final Row row : rows) {
if (row != null && row.getString("name") != null) {
final AsaBlackSubs subs = new AsaBlackSubs();
subs.setId(row.getUUID("id").toString());
subs.setName(row.getString("name"));
subs.setOperatorId(row.getString("operatorId"));
subs.setParams(row.getString("params"));
subs.setDescription(row.getString("description"));
subs.setParentId(row.getString("parentId"));
subs.setOwner(row.getString("owner"));
list.add(subs);
}
}
Collections.sort(list, new HierarchyObjectComparator());
return list;
}
public String create(final AsaBlackSubs subs) {
if (subs.getId() == null) {
final UUID uuid = UUID.randomUUID();
subs.setId(uuid.toString());
}
final Insert query = QueryBuilder.insertInto(CF_NAME)
.value("id", UUID.fromString(subs.getId()))
.value("name", subs.getName())
.value("operatorId", subs.getOperatorId())
.value("params", subs.getParams())
.value("description", subs.getDescription())
.value("owner", subs.getOwner())
.value("parentId", subs.getParentId());
BasicManager.getInstance().getSession().execute(query);
reload();
return subs.getId();
}
}
我也有我的地方對象的緩存,通過運營商來分類的抽象緩存類。
public abstract class OperatorsCache<T extends OperatoredObject> {
protected List<T> cache;
protected Map<String, List<T>> mapByOperator;
public abstract List<T> list();
public void reload() {
cache = new CopyOnWriteArrayList<>(list());
mapByOperator = cache.parallelStream()
.collect(Collectors.groupingBy(OperatoredObject::getOperatorId));
}
public List<T> cachedList() {
return new ArrayList<>(cache);
}
public List<T> cachedListByOperator(final String operatorId) {
return mapByOperator.getOrDefault(operatorId, new ArrayList<>());
}
public T get(final String id) {
final Optional<T> any = cache
.parallelStream()
.filter(obj -> obj.getId().equals(id))
.findAny();
return any.isPresent() ? any.get() : null;
}
}
所以這樣我可以從戰爭和SAR訪問我的課,從任何地方和緩存將是相同的。這個緩存只有一個對象。經理級本身就是在sar中。 這是一個相當不錯的方式,但我知道單身人士的弱點,所以我決定用焊接依賴注入替代我的單身人士,這是集成在Jboss中的。我這樣做:刪除私人構造函數和getInstance()方法,並添加@ApplicationScope註釋。在我的網上課,我只是註釋@Inject。就像這樣:
public class CreateAsaSubsb extends BaseAction {
@Inject
AsaBlackSubsManager manager;
public void perform(final HttpServletRequest request,
final HttpServletResponse response, final JSONObject jsonObject) throws ServletException, IOException, JSONException
...
但是,當我試圖啓動這一切,戰爭應用程序不啓動,我有這樣的例外: org.jboss.weld.exceptions.DeploymentException:類型WELD-001408不滿意的依賴[AsaBlackSubsManager]帶有限定符[@Default]在注入點[[字段] @Inject
我正在尋找答案几天,但我仍然可以弄清楚:我做錯了什麼?
我已經把beans.xml放在戰爭中,在sar中,在jar中放在一個sar中。它沒有幫助。 – 2014-10-29 12:30:08