数据结构中的EJB和存储对象(映射,列表等) [英] EJBs and Storing Objects in a Data Structure (Map, List, etc)

查看:152
本文介绍了数据结构中的EJB和存储对象(映射,列表等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用服务器正常运行期间,是否有可能将对象存储在数据结构中?基本上我想要一个与此数据结构接口的EJB,但不需要一个完整的数据库解决方案。

Is it possible to have objects stored in a data structure for the duration of an App Server's uptime? Basically I want an EJB that interfaces with this Data Structure, but does not require a full fledged database solution.

作为一个例子,我做了这个虚拟的动物对象:

As an example I made this dummy animal object:

package com.test.entities;

public class Animal implements java.io.Serializable {

    private static final long serialVersionUID = 3621626745694501710L;
    private Integer id;
    private String animalName;

    public Integer getId() {
        // TODO Auto-generated method stub
        return id;
    }   
    public void setId(Integer id){
        this.id=id;
    }
    public String getAnimalName(){
        return animalName;

    }
    public void setAnimalName(String animalName){
        this.animalName=animalName;
    }
}

所以这里是 EJB Remote 接口:

So here is the EJB Remote Interface:

package com.test.beans;

import java.util.Map;

import javax.ejb.Remote;

import com.test.entities.Animal;

@Remote
public interface MapBeanRemote {

    public void addAnimal(Animal a);

    public void removaAnimal(Integer id);

    public Animal getAnimalById(Integer id);

    Map<Integer, Animal> getAllAnimals();

}

以下是会话Bean:

package com.test.beans;

import java.util.ConcurrentHashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;

import com.test.entities.Animal;

@Stateless(mappedName="ejb/MapBean")
public class MapBean implements MapBeanRemote{

    Map<Integer, Animal> animalStore;

    @PostConstruct
    public void initialize(){
        animalStore = new ConcurrentHashMap<Integer,Animal>();
    }

    @Override
    public void addAnimal(Animal a) {
        if(a.getId()!=null){
            animalStore.put(a.getId(), a);
        }
    }

    @Override
    public Animal getAnimalById(Integer id) {
        return animalStore.get(id);
    }

    @Override
    public void removaAnimal(Integer id) {
        animalStore.remove(id);

    }

    @Override
    public Map<Integer, Animal> getAllAnimals() {
        return animalStore;
    }

}

所以基本上我想要任何客户想要操纵动物地图来浏览这个EJB,让每个客户端访问相同的精确地图对象。

So basically I want any client who wants to manipulate the Animal Map to go through this EJB and have each client accessing the same exact Map of objects.

这个例子不够好。过了一会儿,所有的动物都被擦除了(我假设当EJB从bean池中被替换)可能会以某种方式注入资源?

This example does not work good enough. After a while all of the animals are erased (I'm assuming when the EJB gets replaced from the bean pool) Could this somehow be injected as a resource?

推荐答案

这可以通过将Map放在Singleton中并从beans访问这个单例来实现。这样,所有EJB实例都有一个实例(因为它们共享相同的类加载器)。不同EAR中的不同会话bean将不会工作,因为它们都有自己的类加载器,但这似乎不是您的方案。

This can be accomplished by putting the Map in a Singleton and accessing this singleton from the beans. That way there is a single instance for all the EJB instances (since they share the same classloader). Different Session beans in different EAR's would not work though as they would each have their own classloader, but that doesn't appear to be your scenario.

您现有的ConcurrentHashMap用法将充分处理大多数情况,但是您仍然需要在addAnimal方法之间进行同步,因为您需要两个方法调用之间的映射一致。

Your existing usage of ConcurrentHashMap will sufficiently handle most of your cases but you still need synchronization around the addAnimal method since you need the map to be consistent between the two method calls.

这篇关于数据结构中的EJB和存储对象(映射,列表等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆