samedi 30 mai 2015

what annotation to create a web service that is created only once when the server starts?

I am stuck on what annotation to use to create a web service where only one instance is created when the server starts I have written a service which contains a time stamp which should be the same time each refresh of the page but the time changes with each local refresh

This is the code I've got

@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;
}

}

and it works apart from the time that is returned with each JSON string is updated with each local refresh, I want to know the annotation required so that it will be the same each time the page is visited

Aucun commentaire:

Enregistrer un commentaire