Hi Tim,

Thanks for taking a look at my code.  I will give what I think is relevent code 
since the entire application is a bit large.  Let me know if you need anything 
else.

Keep in mind I am in a development and debug mode so the code is a bit sloppy 
and inefficient.



destinations-service.xml

   <mbean code="org.jboss.jms.server.destination.Queue"
      name="jboss.messaging.destination:service=Queue,name=requestQueue"
      xmbean-dd="xmdesc/Queue-xmbean.xml">
      <depends 
optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer
      
         
            
            
            
                    
      
        80000
      20000
      20000
   

   <mbean code="org.jboss.jms.server.destination.Queue"
      name="jboss.messaging.destination:service=Queue,name=responseQueue"
      xmbean-dd="xmdesc/Queue-xmbean.xml">
      <depends 
optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer
      
         
            
            
            
         
      
        80000
      20000
      20000
   



ejb-jar.xml

                <message-driven>
                        <ejb-name>CountySearch</ejb-name>
                    
<ejb-class>com.oss.landex.retrieval.ejb.messagebean.CountySearch</ejb-class>
                    <transaction-type>Container</transaction-type>
                    <message-driven-destination>
                       <destination-type>javax.jms.Queue</destination-type>
                    </message-driven-destination>
                </message-driven>
      



jsp that requests data

                try
                {
                        String sessionKey = request.getRequestedSessionId() + 
(100000 + System.currentTimeMillis()%100000);
                    int intCounties = cdCountyData.sendDataRequest(sessionKey, 
dataRequest);
                    dataResponse = cdCountyData.getDataResponse(sessionKey, 
intCounties);
                }
                catch (RemoteException e)
                {
                    e.printStackTrace();
                }




ejb that sends a message to the request queue and gets data from the response 
queue.

    public int sendDataRequest(String inputSessionKey, DataRequest 
inputDataRequest)
    {
        int returnValue = 0;
        String xmlDataRequest = null;
        System.out.print("CountyDataBean.sendDataRequest method started");

        if (inputDataRequest == null || inputDataRequest.getName() == null
                || inputDataRequest.getName().equalsIgnoreCase(""))
        {
        }
        else
        {
            dmcConverter = new DataModelConverter();
            xmlDataRequest = dmcConverter
                    .convertDataRequestObjectToDataRequestXml(inputDataRequest);
            returnValue = sendXmlData(inputSessionKey, xmlDataRequest);
        }
        
        return returnValue;
    }
    
    


    public int sendXmlData(String inputSessionKey, String inputXmlDataRequest)
    {
        int returnValue = 0;
        System.out.print("CountyDataBean.sendXmlData method started");

!!!   --- lot of irrelevant code here   ---   !!!

            try
            {
                ConnectionFactory connectionFactory = null;
                String queueName = "/queue/requestQueue";
                InitialContext initialContext = new InitialContext();
                Queue queue = (Queue) initialContext.lookup(queueName);
                connectionFactory = (ConnectionFactory) 
initialContext.lookup("java:/JmsXA");
                initialContext.close();

                Session session = null;

                connection = connectionFactory.createConnection();
                connection.start();

                // conn = getConnection();
                session = connection.createSession(false, 
Session.AUTO_ACKNOWLEDGE);

                MessageProducer messageProducer = session.createProducer(queue);
                TextMessage textMessage = 
session.createTextMessage(sHttpRequest);

                textMessage.setStringProperty("SessionKey", inputSessionKey);
                textMessage.setStringProperty("CountyId", sRemoteSiteId);
                textMessage.setStringProperty("CountyName", 
remRemoteSite.getCounty());
                
                messageProducer.send(textMessage, DeliveryMode.NON_PERSISTENT, 
9, 60000);
                
                returnValue = returnValue + 1;

                System.out.println("CountyDataBean.sendXmlData -" + 
sHttpRequest + " sent to " + queueName);

                messageProducer.close();
                session.close();
            }
            catch (NamingException e)
            {
                e.printStackTrace();
            }
            catch (JMSException e)
            {
                e.printStackTrace();
            }
            finally
            {
                if (connection != null)
                {
                    try
                    {
                        connection.close();
                    }
                    catch (JMSException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        return returnValue;
    }



    
    public DataResponse getDataResponse(String inputSessionKey, int 
inputcounties)
    {
        DataResponse returnValue = null;

        CountySearchResult csrCountySearchResult = null;
        CountiesSearchResult csrCountiesSearchResult = null;

        returnValue = new DataResponse();
        returnValue.setName("CountiesSearchResult");
        returnValue.setReturnType("CountiesSearchResult Object");

        csrCountiesSearchResult = new CountiesSearchResult();
        DataResponse drDataResponse = null;

        String responseTextMessageText = null;
      
        javax.jms.Connection connection = null;
        
        try
        {
            ConnectionFactory connectionFactory = null;
            String queueName = "/queue/responseQueue";
            InitialContext initialContext = new InitialContext();
            Queue queue = (Queue)initialContext.lookup(queueName);
            connectionFactory = (ConnectionFactory) 
initialContext.lookup("java:/JmsXA");
            initialContext.close();

            Session session = null;

            connection = connectionFactory.createConnection();
            connection.start();

            // conn = getConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            String messageSelector = "SessionKey='" + inputSessionKey + "'";

//            MessageConsumer messageConsumer = session.createConsumer(queue);
            MessageConsumer messageConsumer = session.createConsumer(queue, 
messageSelector);
            javax.jms.Message responseMessage = null;
            BytesMessage responseBytesMessage = null;
            ObjectMessage responseObjectMessage = null;
            TextMessage responseTextMessage = null;
//            String responseTextMessageText = null;
            long startTime = System.currentTimeMillis();
            int messagesExpected = inputcounties;
            long waitTime = 60000; // 60 seconds

            while(messagesExpected > 0 && waitTime > 0 ) 
            {
                responseBytesMessage = null;
                responseObjectMessage = null;
                responseTextMessage = null;
                
                System.out.println("CountyDataBean.getDataResponse Ready to get 
response message");

                responseMessage = messageConsumer.receive(waitTime);

                System.out.println("CountyDataBean.getDataResponse Ready got 
response message");
                

!!!   --- lot of irrelevant code here   ---   !!!



            messageConsumer.close();
            session.close();            
        }
        catch (NamingException e)
        {
            e.printStackTrace();
        }
        catch (JMSException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (JMSException e)
                {
                    e.printStackTrace();
                }
            }
        }
        
        return returnValue;        
    }




Message driven bean


    public void onMessage(Message inputMessage)
    {
        System.out.println("CountySearch.onMessage started");
        
        Session session = null;
        Connection connection = null;

        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        DataOutputStream dataOutputStream = null;
        String inputLine = null;
        StringBuffer responseMessage = null;
        URL urlRemoteSite = null;
        HttpURLConnection httpConnection = null;
        String sServerUrl = null;
        String sPostParameters = null;
        TextMessage requestTextMessage = null;
//        ObjectMessage responseObjectMessage = null;
        TextMessage responseTextMessage = null;

        try
        {
            requestTextMessage = (TextMessage) inputMessage;

            String htmlRequest = requestTextMessage.getText();
            System.out.println("CountySearch.onMessage - message " + htmlRequest
                    + " received");

            if (htmlRequest.indexOf("?") == -1)
            {
                sServerUrl = htmlRequest;
                sPostParameters = "";
            }
            else
            {
                sServerUrl = htmlRequest.substring(0, htmlRequest
                        .indexOf("?"));
                sPostParameters = htmlRequest.substring(htmlRequest
                        .indexOf("?") + 1);
                if (sPostParameters.indexOf("<") == -1)
                {
                }
                else
                {
                    sPostParameters = sPostParameters.substring(0,
                            sPostParameters.indexOf("<"))
                            + URLEncoder.encode(sPostParameters
                                    .substring(sPostParameters.indexOf("<")),
                                    "UTF-8");
                }
            }

            urlRemoteSite = new URL(sServerUrl);

            httpConnection = (HttpURLConnection) urlRemoteSite.openConnection();

            httpConnection.setDoInput(true);
            httpConnection.setDoOutput(true);
            httpConnection.setUseCaches(false);

            // set request method
            httpConnection.setRequestMethod("POST");
            // set request type
            httpConnection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");

            httpConnection.setRequestProperty("Content-Length", sPostParameters
                    .length()
                    + "");

            // Send POST output.
            dataOutputStream = new DataOutputStream(httpConnection
                    .getOutputStream());

            dataOutputStream.writeBytes(sPostParameters);

            dataOutputStream.flush();
            dataOutputStream.close();

            inputStreamReader = new InputStreamReader(httpConnection
                    .getInputStream());

            // Get response data.
            bufferedReader = new BufferedReader(inputStreamReader);

            responseMessage = new StringBuffer("");

            while ((inputLine = bufferedReader.readLine()) != null)
            {
                responseMessage.append(inputLine);
            }

            bufferedReader.close();

            ConnectionFactory connectionFactory = null;
            String queueName = "/queue/responseQueue";
            InitialContext initialContext = new InitialContext();
            Queue queue = (Queue) initialContext.lookup(queueName);
            connectionFactory = (ConnectionFactory) initialContext
                    .lookup("java:/JmsXA");
            initialContext.close();

            connection = connectionFactory.createConnection();
            connection.start();

            // conn = getConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            MessageProducer messageProducer = session.createProducer(queue);
            
            responseTextMessage = 
session.createTextMessage(responseMessage.toString());
            responseTextMessage.setStringProperty("SessionKey", 
requestTextMessage.getStringProperty("SessionKey"));
            responseTextMessage.setStringProperty("CountyId", 
requestTextMessage.getStringProperty("CountyId"));
            responseTextMessage.setStringProperty("CountyName", 
requestTextMessage.getStringProperty("CountyName"));
            messageProducer.send(responseTextMessage, 
DeliveryMode.NON_PERSISTENT, 9, 60000);
            
            System.out.println("CountySearch.onMessage TextMessage -" + 
responseMessage.toString() + " sent to "
                    + queueName);

            messageProducer.close();
            session.close();
        }
        catch (Exception e)
        {
            if (messageDrivenContext == null)
            {            
            }
            else
            {
                messageDrivenContext.setRollbackOnly();
            }
            
            e.printStackTrace();
            System.out.println("The Message Driven Bean failed!");
        }
        finally
        {
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (Exception e)
                {
                    System.out.println("Could not close the connection!" + e);
                }
            }
        }

    }


Hope this helps.

Let me know what else I can do.

Dennis

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3952717#3952717

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3952717

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
JBoss-user mailing list
JBoss-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to