我创建了无状态bean,它具有@Schedule注释的方法。我在servlet中创建了我的bean实例。豆类代码:
@Stateless
public class StockServerBean {
private String price=null;
private ArrayList<String> nasdaqSymbols = new ArrayList<String>();
private String chooseSymb;
public String getChooseSymb() {
return chooseSymb;
}
public void setChooseSymb(String chooseSymb) {
this.chooseSymb = chooseSymb;
}
public StockServerBean(){
// Define some hard-coded NASDAQ symbols
nasdaqSymbols.add("AAPL");
nasdaqSymbols.add("MSFT");
nasdaqSymbols.add("YHOO");
nasdaqSymbols.add("AMZN");
nasdaqSymbols.add("MOT");
}
@Schedule(second="*",minute="*",hour="18,00-22,00", dayOfWeek="Mon-Sun")
public void getQuote(){
if(nasdaqSymbols.indexOf(chooseSymb.toUpperCase()) != -1) {
// Generate a random price for valid symbols
price = (new Double(Math.random()*100)).toString();
}
Session session = null;
Connection conn = null;
ConnectionFactory factory = null;
try{
Context jndiC = new InitialContext();
factory = (ConnectionFactory)jndiC.lookup("MyTestConnectionFactory");
Queue queue = (Queue)jndiC.lookup("MyJMSTestQueue");
conn = factory.createConnection();
conn.start();
session = conn.createSession(false,Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(queue);
TextMessage outM = session.createTextMessage(chooseSymb + " : " + price);
sender.send(outM);
sender.close();
System.out.println("Succesfully placed a price of " + chooseSymb + " :"+ price);
}catch(JMSException je){
System.out.println("Error: " + je.getMessage());
}catch(NamingException ne){
System.out.println("Error: " + ne.getMessage());
ne.printStackTrace();
}finally{
try{
session.close();
conn.close();
}catch(Exception e){
System.out.println("Can't close session/connection :" + e.getMessage());
}
}
}
我的servlet代码:
@WebServlet("/ExecutionServlet")
public class ExecutionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ExecutionServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
StockServerBean bean = new StockServerBean();
bean.setChooseSymb("AAPL");
bean.getQuote();
PrintWriter out = response.getWriter();
out.println("Sending price to the queue");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
结果我得到错误:
无法部署Lesson32部署失败=期间发生错误 部署:加载应用程序时出现异常:EJB Timer Service不是 可用。有关更多详细信息,请参阅server.log。
错误代码:
[2015-10-29T21:53:23.581 + 0200] [glassfish 4.1] [严重] [NCLS-CORE-00026] [javax.enterprise.system.core] [tid: _ThreadID = 47 _ThreadName = admin-listener(4)] [timeMillis:1446148403581] [levelValue:1000] [[生命周期中的异常] 处理java.lang.RuntimeException:EJB Timer Service不是 可用
我添加了属性,因为写了http://docs.oracle.com/cd/E18930_01/html/821-2434/ggrvi.html#glhnl而没有任何改变。
答案 0 :(得分:0)
这很奇怪。由于你的EJB似乎是正确的,并且glassfish试图启动你的计时器方法但是抛出一个更普遍的例外,我认为,glassfish没有正确配置。