我有Jboss AS 6.1以及war和sar应用程序。我用cassandra。所以我需要管理员访问数据库和缓存数据库对象以便快速访问。 所以我这样做了:
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访问我的类,从任何地方和缓存都是一样的。此缓存只有一个对象。经理班本身就在这里。 这是一个相当不错的方法,但我知道单身的弱点,所以我决定用焊接依赖注入替换我的单例,它集成在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
...
但是当我尝试启动所有这些时,war应用程序无法启动,我有这样的例外: org.jboss.weld.exceptions.DeploymentException:WELD-001408类型[AsaBlackSubsManager]的不满意依赖关系,注入点[@ field] @Inject
的限定符[@Default]我几天都在寻找答案,但我仍然可以弄清楚:我做错了什么?
答案 0 :(得分:0)
JBoss AS 6是一个Java EE 6应用服务器。根据{{3}}(它是Java EE 6的一部分),您必须在您的存档中放置一个beans.xml文件:在WEB-INF /目录中进行战争,在META-INF /目录中一个罐子。