newbie question - servlets and JMS

Hi Guys

Im trying to write a servlet that receives a File and then sends it using JMS.... i have some basic code in place (from examples ive fouond) ...

Ive written 2 classes

1) a servlet that receives the file

2) a requester that sends the data to the JMS Queue

But there is a couple of things im not sure of ( see code below)....

1) How do i instantiate the 'connection' that im passing into the newRequester method of the JMSRequester class?

2) What sort of strings do i pass into newRequester for the other params?

3) Is it ok to place a FILE object as a parameter of the textMessage.setObjectProperty call?

4) Is my solution ok? Am i on track or am I way off the mark?

5) What JAR do i need for the JndiUtil class?

Thanks for any help

Marty

package lmc.servlet;

import lmc.servlet.JMSRequester;

import java.io.File;

import java.io.IOException;

import java.util.List;

import javax.jms.Connection;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Level;

import org.apache.log4j.Logger;

publicclass PATFServletextends HttpServlet{

private Logger logger = Logger.getLogger(PATFServlet.class.getName());

publicstaticvoid main(String[] args){

// TODO Auto-generated method stub

}

publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

String filename = request.getParameter("Filename");

File patfDetail = (File) request.getAttribute("PATFDetail");

Connection connection =null;

String requestQueueName ="abc";

String replyQueueName ="abc";

String invalidQueueName ="abc";

try{

JMSRequester jmsRequest = JMSRequester.newRequester(connection, requestQueueName,

replyQueueName,

invalidQueueName);

jmsRequest.send(patfDetail, filename);

}catch (Exception e){

}

}

}

====================================

package lmc.servlet;

import java.io.File;

import javax.jms.Connection;

import javax.jms.Destination;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageConsumer;

import javax.jms.MessageProducer;

import javax.jms.Session;

import javax.jms.TextMessage;

import javax.naming.NamingException;

publicclass JMSRequester{

private Session session;

private Destination replyDestinationQueue;

private MessageProducer requestMessageProducer;

private MessageConsumer replyMessageConsumer;

private MessageProducer invalidMessageProducer;

/*

* Default Constructor

*/

protected JMSRequester(){

super();

}

/*

* Static method that creates an instance of the JMSRequester class

* and

*/

publicstatic JMSRequester newRequester(Connection connection,

String requestQueueName,

String replyQueueName,

String invalidQueueName)

throws JMSException, NamingException{

JMSRequester jmsRequestor =new JMSRequester();

jmsRequestor.initialise(connection, requestQueueName,

replyQueueName, invalidQueueName);

return jmsRequestor;

}

/*

* In initialize, the requestor uses the Connection and queue names to

* connect to the messaging system.

*/

protectedvoid initialise(Connection connection,

String requestQueueName,

String replyQueueName,

String invalidQueueName)

throws NamingException, JMSException{

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

Destination requestQueue = JndiUtil.getDestination(requestQueueName);

Destination invalidQueue = JndiUtil.getDestination(invalidQueueName);

replyDestinationQueue = JndiUtil.getDestination(replyQueueName);

requestMessageProducer = session.createProducer(requestQueue);

invalidMessageProducer = session.createProducer(invalidQueue);

replyMessageConsumer= session.createConsumer(replyDestinationQueue);

}

/*

* This method sends the object to the JMS Queue.

*/

publicvoid send(File patfDetail, String patfFilename)throws JMSException{

TextMessage textMessage = session.createTextMessage();

textMessage.setText("Hello world.");

textMessage.setJMSReplyTo(replyDestinationQueue);

textMessage.setObjectProperty(patfFilename, patfDetail);

requestMessageProducer.send(textMessage);

//print out details of the message

System.out.println("Sent request");

System.out.println("\tTime:" + System.currentTimeMillis() +" ms");

System.out.println("\tMessage ID: " + textMessage.getJMSMessageID());

System.out.println("\tCorrel. ID: " + textMessage.getJMSCorrelationID());

System.out.println("\tReply to:" + textMessage.getJMSReplyTo());

System.out.println("\tContents:" + textMessage.getText());

}

/*

* This method allows the class to receive reply messages.

*/

publicvoid receiveSync()throws JMSException{

Message msg = replyMessageConsumer.receive();

if (msginstanceof TextMessage){

TextMessage replyMessage = (TextMessage) msg;

System.out.println("Received reply ");

System.out.println("\tTime:" + System.currentTimeMillis() +" ms");

System.out.println("\tMessage ID: " + replyMessage.getJMSMessageID());

System.out.println("\tCorrel. ID: " + replyMessage.getJMSCorrelationID());

System.out.println("\tReply to:" + replyMessage.getJMSReplyTo());

System.out.println("\tContents:" + replyMessage.getText());

}else{

System.out.println("Invalid message detected");

System.out.println("\tType:" + msg.getClass().getName());

System.out.println("\tTime:" + System.currentTimeMillis() +" ms");

System.out.println("\tMessage ID: " + msg.getJMSMessageID());

System.out.println("\tCorrel. ID: " + msg.getJMSCorrelationID());

System.out.println("\tReply to:" + msg.getJMSReplyTo());

//resend the message...

msg.setJMSCorrelationID(msg.getJMSMessageID());

invalidMessageProducer.send(msg);

}

}

}

[10134 byte] By [mlynch147a] at [2007-11-27 9:45:49]
# 1

>

> 1) How do i instantiate the 'connection' that im

> passing into the newRequester method of the

> JMSRequester class?

Create a JMS connection using a JNDI lookup in your managed environment or directly using the JMS API. See the JMS tutorial.

> 2) What sort of strings do i pass into newRequester

> for the other params?

Those strings are queue/topic names that you have created in your JMS server.

> 3) Is it ok to place a FILE object as a parameter of

> the textMessage.setObjectProperty call?

I assume you are referring to java.io.File object? You should be ok since the File object simply represents an abstract path name. May be problematic if passing to a different server/platform.

> 4) Is my solution ok? Am i on track or am I way off

> the mark?

You're getting there. Without too much analysis at least you you need to address session maintenance

> 5) What JAR do i need for the JndiUtil class?

>

JndiUtil is not standard JMS class. Are you using a third party library?

axlerunnera at 2007-7-12 23:55:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...