SendPrescriptionServlet.java
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:

Java编程

开发平台:

Java

  1. package bible.ejb.message.ui;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.lang.*;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import java.rmi.RemoteException;
  8. import java.util.*;
  9. import javax.ejb.SessionBean;
  10. import javax.ejb.SessionContext;
  11. import javax.ejb.EJBException;
  12. import javax.naming.*;
  13. import javax.jms.*;
  14. public class SendPrescriptionServlet extends HttpServlet {
  15.     SessionContext      sc = null;
  16.     QueueConnection     queueConnection = null;
  17.     Queue               queue = null;
  18.   public void init( ServletConfig config ) throws ServletException {
  19.         Context context = null;
  20.         QueueConnectionFactory queueConnectionFactory = null;
  21.         try {
  22.             context = new InitialContext();
  23.       queue = (Queue)
  24.                 context.lookup("java:comp/env/jms/PrescriptionQueue");
  25.             // Create a TopicConnection
  26.             queueConnectionFactory = (QueueConnectionFactory)
  27.             context.lookup("java:comp/env/jms/PrescriptionQueueConnectionFactory");
  28.             queueConnection =
  29.                 queueConnectionFactory.createQueueConnection();
  30.         } catch (Throwable t) {
  31. //           log.append(t.toString());
  32.         }
  33.     }
  34.    public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException
  35.   {
  36.       QueueSession   queueSession = null;
  37.         QueueSender queueSender = null;
  38.         TextMessage    message = null;
  39.         String pageToReturn = "";
  40.         try {
  41.             // retrieving parameters from request
  42.         String physcianId = (String)req.getParameter("physcianId");
  43.           String patientId = (String)req.getParameter("patientId");
  44.         String date = (String)req.getParameter("date");
  45.         String drug = (String)req.getParameter("drug");
  46.         String note = (String)req.getParameter("note");
  47.             // creating a jms session
  48.             queueSession =
  49.                 queueConnection.createQueueSession(true, 0);
  50.       queueSender = queueSession.createSender(queue);
  51.       // loading up message
  52.             message = queueSession.createTextMessage();
  53.             message.setStringProperty("neccesity", "immediate");
  54.             message.setText(generatePrescriptionXML(physcianId, patientId, date,drug, note ));
  55.       // sending JMS message to queue
  56.             queueSender.send(message,
  57.                                 javax.jms.DeliveryMode.NON_PERSISTENT,
  58.                                 javax.jms.Message.DEFAULT_PRIORITY,
  59.                                 2000);
  60.       pageToReturn = "messageSent.jsp";
  61.         } catch (Exception e) {
  62.           pageToReturn = "error.jsp";
  63.             e.printStackTrace();
  64.         }
  65.          getServletContext().getRequestDispatcher(pageToReturn).forward(req, res);
  66.   }
  67.     //XML Representation of a prescription
  68.   private String generatePrescriptionXML(String physcianId, String patientId, String date,String drug, String note )
  69.     {
  70.         StringBuffer sb = new StringBuffer("<?xml version="1.0" ?>");
  71.       sb.append("<PRESCRIPTION>");
  72.         sb.append("<PHYSICIAN_ID>" + physcianId + "</PHYSICIAN_ID>");
  73.         sb.append("<PATIENT_ID>" + patientId + "</PATIENT_ID>");
  74.         sb.append("<DRUG>" + drug + "</DRUG>");
  75.         sb.append("<NOTE>" + note + "</NOTE>");
  76.         sb.append("</PRESCRIPTION>");
  77.         return sb.toString();
  78.     }
  79.    // close the queue connection here
  80.     public void destroy()
  81.   {
  82.       if (queueConnection != null) {
  83.                 try {
  84.                     queueConnection.close();
  85.                 } catch (Exception e) {
  86.                     e.printStackTrace();
  87.                 }
  88.             }
  89.   }
  90. }