[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to Store a Text File in JMS Queue

Posted on 2006-05-30
9
Medium Priority
?
881 Views
Last Modified: 2008-01-16
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
0
Comment
Question by:ramsin112400
  • 4
  • 2
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 16794048
try passing it as a byte array.

ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[512];
int n = 0;
while ((n=stream.read(buf))>=0) {
   out.write(buf, o, n);
}
out.close();
message.setObject(out.toByteArray());
0
 
LVL 6

Assisted Solution

by:phuocnh
phuocnh earned 500 total points
ID: 16794075
Your problem is stream.toString() doesn't return the content the text file.
It should be:
String text = new String(fileData);
Now your code should be:
FormFile myFile                    =      pdpFileUploadForm.getTheFile();
byte[] fileData                       =      myFile.getFileData();
String text = new String(fileData);
/*
  -----------------------------------------------
   JMS Code
   ------------------------------------------------
*/
       ConnectionFactory connectionFactory = null;
        javax.jms.Connection connection = null;
        javax.jms.Session session = null;
        Destination destination = null;
        MessageProducer messageProducer = null;
        TextMessage message = 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.setText(text);
            messageProducer.send(message);
       }
      catch (Exception ex)
     {
         ex.printStackTrace();
     }
Phuoc
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16794096
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Author Comment

by:ramsin112400
ID: 16794381
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
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16794524
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("");
          }
     }
}
0
 
LVL 6

Expert Comment

by:phuocnh
ID: 16794539
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 16794550
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);
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question