创建仅在服务器启动时创建一次的Web服务的注释是什么?

时间:2015-05-30 20:38:54

标签: java web-services java-ee

我坚持使用什么注释来创建一个Web服务,其中只有一个实例在服务器启动时创建我已经编写了一个服务,其中包含一个时间戳,该时间戳应该是每次刷新页面的时间,但是时间每次本地刷新都会发生变化

这是我的代码

@Path("/message")
@Startup
@Singleton
public class TestEndPoint
{
@Inject ConfigurationService configurationService;
@GET

@Path("/test")
@Produces("application/json")

public String printMessage()
{
    //ConfigurationService configurationService = new
    ConfigurationService();
    return configurationService.getPropertiesJSONString();
}
}

public class ConfigurationService
{
@Inject Configuration configuration;
private String propertiesJSONString;

public String getPropertiesJSONString()
{
    try
    {
        Properties properties = configuration.getProperties();
        ObjectMapper objectMapper = new ObjectMapper();
        propertiesJSONString = objectMapper.writeValueAsString(properties);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    return propertiesJSONString;
}

public void setPropertiesJSONString(String propertiesJSONString)
{
    this.propertiesJSONString = propertiesJSONString;
}

public ConfigurationService()
{

}
}

public class Configuration implements Serializable
{

private Properties properties;

public Configuration()
{
    // TODO Auto-generated constructor stub
}

public Properties getProperties() throws IOException
{

    Properties prop = new Properties();
    String propFileName = System.getProperty("propertiesfilelocation");
    InputStream inputStream = null;

    inputStream = new FileInputStream(propFileName);

    if (inputStream != null)
    {
        prop.load(inputStream);
    }
    else
    {

    }

    Date date = new Date();
    SimpleDateFormat formatter = 
    new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");
    String formattedDate = formatter.format(date);
    prop.put("time", formattedDate);

    return prop;
}

public void setProperties(Properties properties)
{
    this.properties = properties;
}

}

并且它与每个本地刷新更新每个JSON字符串时返回的时间不同,我想知道所需的注释,以便每次访问页面时它都是相同的

1 个答案:

答案 0 :(得分:1)

您不会在实例属性中保存Properties对象,并在每个请求中生成一个新对象。这就是为什么它不能像你期望的那样工作。

只需将getProperties方法更改为使用延迟初始化:

public Properties getProperties() throws IOException
{
    if(properties != null)
        return properties;

    //else, initialize
    properties = new Properties();
    String propFileName = System.getProperty("propertiesfilelocation");
    InputStream inputStream = null;

    inputStream = new FileInputStream(propFileName);

    if (inputStream != null)
        properties.load(inputStream);

    Date date = new Date();
    SimpleDateFormat formatter = 
    new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");
    String formattedDate = formatter.format(date);
    properties.put("time", formattedDate);

    return properties;
}

通过这种方式,它只会被初始化一次而不是每次请求。

编辑:如果您不希望ConfigurationService只检索一次属性,则可以将该模式应用于TestEndPoint并保存JSON字符串。例如:

public class TestEndPoint
{
    @Inject ConfigurationService configurationService;

    private String jsonProperties = null;

    //left out other annotations
    public String printMessage()
    {
        if (jsonProperties == null)
            jsonProperties = configurationService.getPropertiesJSONString()

        return jsonProperties;
    }
}

如果您不想搞乱现有服务,那将更合适。