Link to home
Start Free TrialLog in
Avatar of ramsin112400
ramsin112400

asked on

How to Store a Text File in JMS Queue

Hi All,

I'm uploading a text file and would like to store this text file as an object (or in some other way) in the JMS Queue.  For this i'm passing the Input stream to the setObject method ()
( message.setObject(stream .toString()); ) which is not working out.


What i'm doing is right?. Or how to store a text file in a JMS queue and how to retrieve the same in Message Driven Bean?

Struts Code
--------------

PDPFileUploadForm pdpFileUploadForm =    (PDPFileUploadForm) form;

FormFile myFile                      =       pdpFileUploadForm.getTheFile();
String  fileType                     =      pdpFileUploadForm.getFileType();
String contentType                      =       myFile.getContentType();
String fileName                         =       myFile.getFileName();
int fileSize                            =       myFile.getFileSize();
 byte[] fileData                         =       myFile.getFileData();
InputStream stream                      =       myFile.getInputStream();


-----------------------------------------------
JMS Code
_________________________________
       // Context jndiContext = null;
        ConnectionFactory connectionFactory = null;
        javax.jms.Connection connection = null;
        javax.jms.Session session = null;
        Destination destination = null;
        MessageProducer messageProducer = null;
        //TextMessage message = null;
        ObjectMessage message = null;
        // String fileName2 = null;

       try
      {
               InitialContext ctx =getInitialContext();
              connectionFactory =(ConnectionFactory) ctx.lookup("ConnectionFactory");
              destination =(Queue) ctx.lookup("queue/messageQueue");
              connection = connectionFactory.createConnection();
              session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
              messageProducer = session.createProducer(destination);
             // message = session.createTextMessage();
             message = session.createObjectMessage();
              
             // message.setText(line);
             message.setObject(stream .toString());
            //  message.setObject(stream.toString());
              
              messageProducer.send(message);
---------------------------------------------------------------------------------------------------
Can anybody help me ..how to store a text file in the JMS Queue ?.

Thanks,
Ram
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You should check the size of the message which JMS provider can accept. If size of your file is bigger than the limit you should change the limit or split your file.
Phuoc
Avatar of ramsin112400
ramsin112400

ASKER

My text file is looking like this ::

Customer.txt (Text file may contain 'n' number of lines, the number of lines is unknown))
---------------
First Name | Last Name | Street            | City          | State  | Country | ZIP
John1       | Williams1    | 550 Market Street  | San Francisco | CA    | USA     | 95204 |
John2      | Williams2   | 550 Market Street2 | San Francisco | CA2    | USA     | 95204 |
.
.
.
.
.

if we put the entire file as a string in the JMS Queue, the other side Message Driven Bean will pick up the whole content of the file as one String
(String line = (String) ((TextMessage)msg2).getText();)

Then how can i get each line of the text file and inturn each token of each line

thanks,
ram
Yeah. This is another problem: parsing text file.
import java.util.*;
class Parser {
     public static void main(String[] args)
               throws IOException{
             String text = "yourtext";
 
           ArrayList record = new ArrayList();
           String[] list= text.split("\n");
           for (int i=0;i<list.length;i++)
           {
               String out = (String) list[i];
               //Adding cells to the ArrayList cells
               ArrayList cells = new ArrayList();
               StringTokenizer tokens = new StringTokenizer(out,"|");
               while(tokens.hasMoreElements()){
                    cells.add(tokens.nextToken());
               }
               record.add(cells);
          }
          //Iterating thro the Records
          ListIterator recordIterator = record.listIterator();
          while(recordIterator.hasNext())     {
               ArrayList cell = (ArrayList)recordIterator.next();
               ListIterator cellIterator = cell.listIterator();
               while(cellIterator.hasNext()){
                    System.out.print((String)cellIterator.next()+" ");
               }
               System.out.println("");
          }
     }
}
Every line of text is terminated by "\n".
You can replace "|" by comma (;) then using http://ostermiller.org/utils/CSV.html
to parse it.
Phuoc.
Passing the file as a byte array as I showed above would allow you to create a stream at the other end and read it with a BufferedReader

byte[] file = ....
BufferedReader stream = new BufferedReader(new InputStreamReader(new ByteInputStream(file)));
String line = null;
while (null!=(line = stream.readLine())) {
    System.out.println(line);
}