Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

File Tranfer Issue Using JMS

908647Jan 3 2012 — edited Jan 4 2012
Hello everybody,

I am new to JMS and trying to develop a chat application using Swing and JMS. TextMessaging is working fine but i am unable to configure FileTranfer.

Configurations are stated below:
Apache Geronimo 2.2 (as JMS provider server).
ActiveMQ (for connecting to JMS server).
Java Swing.

Establishing Connection:

Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
env.put(Context.PROVIDER_URL, "tcp://localhost:61616?jms.prefetchPolicy.all=100&jms.redeliveryPolicy.maximumRedeliveries=5");
env.put(Context.SECURITY_PRINCIPAL, "system");
env.put(Context.SECURITY_CREDENTIALS, "manager");
env.put("topic.myTopic","org.apache.geronimo.configs/activemq-ra/JCAAdminObject/myTopic");
// create publisher and subscriber
InitialContext ctx = new InitialContext(env);
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) ctx.lookup("TopicConnectionFactory");
final Topic topic = (Topic) ctx.lookup("myTopic");
ActiveMQConnection conn = (ActiveMQConnection)factory.createTopicConnection();

// Downloaded Apache FtpServer1.0.6 and started the server from bin folder ftpd.bat.
conn.getBlobTransferPolicy().setUploadUrl("http://localhost:2121/fileserver/");

final ActiveMQSession session = (ActiveMQSession)conn.createSession(false,Session.AUTO_ACKNOWLEDGE);
final TopicPublisher publisher = session.createPublisher(topic);
subscriber = session.createSubscriber(topic);


Sending BlobMessage

String filePath = fileChooser.getSelectedFile().getAbsolutePath();
File file = new File(filePath);
BlobMessage msg = session.createBlobMessage(file);
String sender = System.getProperty("user.name");
msg.setStringProperty("sender", sender);
msg.setStringProperty("filename", file.getName());
publisher.publish(msg);


Receiving BlobMessage

public void onMessage(Message m) {
try {
String sender = m.getStringProperty("sender");
if(m instanceof TextMessage){
TextMessage msg = (TextMessage) m;
incoming.append(sender + "> " + msg.getText() + "\n");
} else if (m instanceof BlobMessage) {
String filename = m.getStringProperty("filename");
JOptionPane.showMessageDialog(null, "You have recieved a file "+filename+" from "+sender+" !!!");
BlobMessage blobMessage = (BlobMessage) m;
InputStream inputStream = blobMessage.getInputStream();
if (inputStream != null) {
try {
OutputStream os = new FileOutputStream(new File("C://"+filename));
try {
byte[] buffer = new byte[4096];
for (int n; (n = inputStream.read(buffer)) != -1; )
os.write(buffer, 0, n);
}catch(Exception e){
e.printStackTrace();
}
finally { os.close(); }
}catch(Exception e){
e.printStackTrace();
}
finally { inputStream.close(); }

}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}


I downloaded Apache FtpServer1.0.6 and started the server from bin folder ftpd.bat as I found this step after googling for sometime.
I am getting the Recieved File alert in OnMessage method but the inputstream is null. No data is coming to the reciever. I am unable to figure out the actual issue.

Please help me out.

Many Thanks
Rupal Chatterjee

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 1 2012
Added on Jan 3 2012
2 comments
1,787 views