I created Stateless bean, which has method with @Schedule annotation. I create instance of my bean in servlet. Code of 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());
}
}
}
Code of my 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);
In result I obtain error:
cannot Deploy Lesson32 deploy is failing=Error occurred during deployment: Exception while loading the app: EJB Timer Service is not available. Please see server.log for more details.
Code of error:
[2015-10-29T21:53:23.581+0200] [glassfish 4.1] [SEVERE] [NCLS-CORE-00026] [javax.enterprise.system.core] [tid: _ThreadID=47 _ThreadName=admin-listener(4)] [timeMillis: 1446148403581] [levelValue: 1000] [[ Exception during lifecycle processing java.lang.RuntimeException: EJB Timer Service is not available
I add property, as writed http://docs.oracle.com/cd/E18930_01/html/821-2434/ggrvi.html#glhnl and nothing changed.