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.getTheFi le();
String fileType = pdpFileUploadForm.getFileT ype();
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("ConnectionFact ory");
destination =(Queue) ctx.lookup("queue/messageQ ueue");
connection = connectionFactory.createCo nnection() ;
session = connection.createSession(f alse, Session.AUTO_ACKNOWLEDGE);
messageProducer = session.createProducer(des tination);
// message = session.createTextMessage( );
message = session.createObjectMessag e();
// message.setText(line);
message.setObject(stream .toString());
// message.setObject(stream.t oString()) ;
messageProducer.send(messa ge);
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---
Can anybody help me ..how to store a text file in the JMS Queue ?.
Thanks,
Ram
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.getTheFi
String fileType = pdpFileUploadForm.getFileT
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("ConnectionFact
destination =(Queue) ctx.lookup("queue/messageQ
connection = connectionFactory.createCo
session = connection.createSession(f
messageProducer = session.createProducer(des
// message = session.createTextMessage(
message = session.createObjectMessag
// message.setText(line);
message.setObject(stream .toString());
// message.setObject(stream.t
messageProducer.send(messa
--------------------------
Can anybody help me ..how to store a text file in the JMS Queue ?.
Thanks,
Ram
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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).getTex t();)
Then how can i get each line of the text file and inturn each token of each line
thanks,
ram
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).getTex
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.hasMoreElemen ts()){
cells.add(tokens.nextToken ());
}
record.add(cells);
}
//Iterating thro the Records
ListIterator recordIterator = record.listIterator();
while(recordIterator.hasNe xt()) {
ArrayList cell = (ArrayList)recordIterator. next();
ListIterator cellIterator = cell.listIterator();
while(cellIterator.hasNext ()){
System.out.print((String)c ellIterato r.next()+" ");
}
System.out.println("");
}
}
}
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.hasMoreElemen
cells.add(tokens.nextToken
}
record.add(cells);
}
//Iterating thro the Records
ListIterator recordIterator = record.listIterator();
while(recordIterator.hasNe
ArrayList cell = (ArrayList)recordIterator.
ListIterator cellIterator = cell.listIterator();
while(cellIterator.hasNext
System.out.print((String)c
}
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.
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);
}
byte[] file = ....
BufferedReader stream = new BufferedReader(new InputStreamReader(new ByteInputStream(file)));
String line = null;
while (null!=(line = stream.readLine())) {
System.out.println(line);
}
Phuoc