启动Tomcat后第一次调用REST非常慢

时间:2016-03-15 13:06:23

标签: java rest tomcat java-ee jersey

My Rest服务运行良好,但是当我启动Tomcat服务器并首先调用Rest时,它意识到3-4秒。另一个叫Rest很快就像2-30毫秒。 我认为那是因为在第一次调用时我登录到数据库或启动事务(EntityManager)。 [右?]

我的web.xml:

Locale.ENGLISH

persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>AggregatorGroupRest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.agg.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

我的休息实施

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="EmployeeService">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>    
    <class>com.agg.atomic.model.Employee</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
            <properties>
                <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
                <property name="javax.persistence.jdbc.url"    value="jdbc:mysql://db4free.net:3306/qwqwqwqw?useSSL=false" />       
                <property name="javax.persistence.jdbc.user" value="login" />
                <property name="javax.persistence.jdbc.password" value="password" />
                <property name="eclipselink.ddl-generation" value="create-tables" />
                <property name="eclipselink.ddl-generation.output-mode" value="database" />
        </properties>
    </persistence-unit>
</persistence>

生成器:

@Path("/test")
public class TestServiceImpl implements TestService{

    @GET
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public EmployeeModel getEmployee() {
        TestBuilder testBuilder = new TestBuilder();
        return testBuilder.getEmployee();
    }
}

并实现EntityManager:

public class TestBuilder {
    private static EntityManager entityManager = EntityManagerUtil.getEntityManager();

    public EmployeeModel getEmployee(){
        if (!entityManager.getTransaction().isActive()) entityManager.getTransaction().begin();
        Employee employee = entityManager.find(Employee.class, 6);

        EmployeeModel result = TestResultMapping.getEmployee(employee);

        return result;
    }
}

现在问题: 我能做的就是我第一次打电话给REST会跟他后的其他人一样快。

感谢您的时间。

0 个答案:

没有答案