SendPrescriptionServlet.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:
Java编程
开发平台:
Java
- package bible.ejb.message.ui;
- import java.io.*;
- import java.util.*;
- import java.lang.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.rmi.RemoteException;
- import java.util.*;
- import javax.ejb.SessionBean;
- import javax.ejb.SessionContext;
- import javax.ejb.EJBException;
- import javax.naming.*;
- import javax.jms.*;
- public class SendPrescriptionServlet extends HttpServlet {
- SessionContext sc = null;
- QueueConnection queueConnection = null;
- Queue queue = null;
- public void init( ServletConfig config ) throws ServletException {
- Context context = null;
- QueueConnectionFactory queueConnectionFactory = null;
- try {
- context = new InitialContext();
- queue = (Queue)
- context.lookup("java:comp/env/jms/PrescriptionQueue");
- // Create a TopicConnection
- queueConnectionFactory = (QueueConnectionFactory)
- context.lookup("java:comp/env/jms/PrescriptionQueueConnectionFactory");
- queueConnection =
- queueConnectionFactory.createQueueConnection();
- } catch (Throwable t) {
- // log.append(t.toString());
- }
- }
- public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException
- {
- QueueSession queueSession = null;
- QueueSender queueSender = null;
- TextMessage message = null;
- String pageToReturn = "";
- try {
- // retrieving parameters from request
- String physcianId = (String)req.getParameter("physcianId");
- String patientId = (String)req.getParameter("patientId");
- String date = (String)req.getParameter("date");
- String drug = (String)req.getParameter("drug");
- String note = (String)req.getParameter("note");
- // creating a jms session
- queueSession =
- queueConnection.createQueueSession(true, 0);
- queueSender = queueSession.createSender(queue);
- // loading up message
- message = queueSession.createTextMessage();
- message.setStringProperty("neccesity", "immediate");
- message.setText(generatePrescriptionXML(physcianId, patientId, date,drug, note ));
- // sending JMS message to queue
- queueSender.send(message,
- javax.jms.DeliveryMode.NON_PERSISTENT,
- javax.jms.Message.DEFAULT_PRIORITY,
- 2000);
- pageToReturn = "messageSent.jsp";
- } catch (Exception e) {
- pageToReturn = "error.jsp";
- e.printStackTrace();
- }
- getServletContext().getRequestDispatcher(pageToReturn).forward(req, res);
- }
- //XML Representation of a prescription
- private String generatePrescriptionXML(String physcianId, String patientId, String date,String drug, String note )
- {
- StringBuffer sb = new StringBuffer("<?xml version="1.0" ?>");
- sb.append("<PRESCRIPTION>");
- sb.append("<PHYSICIAN_ID>" + physcianId + "</PHYSICIAN_ID>");
- sb.append("<PATIENT_ID>" + patientId + "</PATIENT_ID>");
- sb.append("<DRUG>" + drug + "</DRUG>");
- sb.append("<NOTE>" + note + "</NOTE>");
- sb.append("</PRESCRIPTION>");
- return sb.toString();
- }
- // close the queue connection here
- public void destroy()
- {
- if (queueConnection != null) {
- try {
- queueConnection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }