使用调度程序在服务器启动功能上调用方法EJB时为空指针

时间:2018-10-27 01:37:27

标签: java-ee jboss ejb wildfly

大家好,我在尝试启动服务器WildFly时尝试达到方法EJB,并且我希望每5秒执行一次该方法,这就是为什么我使用类Schedular的原因,但是问题是,当我执行服务器时,此异常出现在我身上< / p>

  

02:12:54,738错误[stderr](Timer-2)javax.naming.NameNotFoundException:Map-JavaEE-ear / Map-JavaEE-ejb / ProjectService!tn.esprit.Map.interfaces.ProjectRemote-服务jboss .naming.context.java.Map-JavaEE-ear.Map-JavaEE-ejb。“ ProjectService!tn.esprit.Map.interfaces.ProjectRemote”   02:12:54,738错误[stderr](Timer-2)在org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)   02:12:54,738错误[stderr](Timer-2)在org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)   02:12:54,738 org.jboss.as.naming.InitialContext $ DefaultInitialContext.lookup(InitialContext.java:235)出现[stderr](Timer-2)   02:12:54,738 org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)出现[stderr](Timer-2)错误   02:12:54,738错误[stderr](Timer-2)在org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)   02:12:54,738 javax.naming.InitialContext.lookup(未知源)出现[stderr](Timer-2)错误   02:12:54,738 javax.naming.InitialContext.lookup(未知源)出现[stderr](Timer-2)错误   02:12:54,738在tn.esprit.Map.utilities.Schedular.run(Schedular.java:23)出现[stderr](Timer-2)错误   02:12:54,738 java.util.TimerThread.mainLoop中的[stderr](Timer-2)错误(未知源)   02:12:54,738 java.util.TimerThread.run(未知源)出现[stderr](Timer-2)

这是我的ClassService

@Stateless
public class ProjectService implements ProjectRemote {
@PersistenceContext(unitName = "MAP")
private EntityManager em;

@Override
public List<Project> getAllProjects() {
    Query query = em.createQuery(
            "SELECT p.id ,p.projectName , p.startDate" + " , p.endDate , p.address , p.totalNumberResource ,"
                    + " p.levioNumberResource,p.picture, p.projectType  FROM Project p");
    List<Object[]> res = query.getResultList();
    List<Project> projects = new ArrayList<Project>();
    res.forEach(array -> {
        Project project = arrayToProject(array);
        projects.add(project);
    });

    return projects;
}

public Project arrayToProject(Object[] array){
    Project project = new Project();
    project.setId((int) array[0]);
    project.setProjectName((String) array[1]);
    project.setStartDate((Date) array[2]);
    project.setEndDate((Date) array[3]);
    project.setAddress((String) array[4]);
    project.setTotalNumberResource((int) array[5]);
    project.setLevioNumberResource((int) array[6]);
    project.setPicture((String) array[7]);
    project.setProjectType((ProjectType) array[8]);

    return project;
}
    @Override
public String archiveProject() throws ParseException {
    List<Project> projects = this.getAllProjects();
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    ArchivedProjects archivedProjects = new ArchivedProjects();
    for (Project project : projects) {
        archivedProjects.setId(project.getId());
        archivedProjects.setProjectName(project.getProjectName());
        archivedProjects.setProjectType(project.getProjectType());
        archivedProjects.setAddress(project.getAddress());
        archivedProjects.setClient(project.getClient());
        archivedProjects.setStartDate(project.getStartDate());
        archivedProjects.setEndDate(project.getEndDate());
        archivedProjects.setLevioNumberResource(project.getLevioNumberResource());
        archivedProjects.setTotalNumberResource(project.getTotalNumberResource());
        archivedProjects.setOrganizationalChart(project.getOrganizationalChart());
        archivedProjects.setPicture(project.getPicture());
        if ((dateFormat.format(date).compareTo(project.getEndDate().toString()) > 0)) {
            em.remove(em.contains(project) ? project : em.merge(project));
            em.persist(em.contains(archivedProjects) ? archivedProjects : em.merge(archivedProjects));
        }

        System.out.println(project.getEndDate().toString() + "   " + dateFormat.format(date).toString());
    }
    return "Archived";

}

我的定期班级:

@ManagedBean
public class Schedular extends TimerTask{
@EJB
ProjectRemote projectRemote;
@Override
public void run() {

    try {
        projectRemote.archiveProject();
    } catch (ParseException e) {
        e.printStackTrace();
    }

}
}

最后是我调用EJB方法的地方:

@SuppressWarnings("serial")
public class StartUpService extends HttpServlet {

Schedular sc = new Schedular();
public void init() throws ServletException {
    Timer timer = new  Timer()  ;
    timer.scheduleAtFixedRate(sc,5000,1000);
}
}

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

您可以将以下单例添加到EJB模块中(或在archiveProject方法中添加计划注释)

@Singleton
    public class ProjectArchivingScheduler {

        @EJB
        ProjectRemote projectRemote;

        @Schedule(minute = "*", hour = "*", second = "*/5", persistent = false)
        public void refresh() {
            projectRemote.archiveProject();
        }
    }

答案 1 :(得分:1)

您必须在服务中添加注释LocalBean。

import javax.ejb.LocalBean;

@Stateless
@LocalBean
public class ProjectService implements ProjectRemote {
@PersistenceContext(unitName = "MAP")
private EntityManager em;
/**/
}