This section provides information about creating, sending, and receiving BLOB messages.
Creating BLOB messages – new method, createBlobMessage(), has been added to com.sybase.jms.client class. Use SybSession to create BLOB messages.
Sending BLOB messages – either:
Send by URL – use the following code to send a file or URL that exists on a shared file system or Web server, around the Java Message Service (JMS) network:
BlobMessage message = session.createBlobMessage(new URL("http://some.shared.site.com")); producer.send(message);
Send by file or input stream – you are creating files or streams dynamically on the client, you may want to upload the file to some server such as Jetty, FTP, and so forth. Use one of these methods:
Upload the file using a Web application:
//set upload url,fileserver is file uploaded using web application ((SybConnection)connection).setRemoteUploadURL("http://hostname/8000:/fileserver/");
Use a local file:
// lets use a local file BlobMessage message = session.createBlobMessage(new File("d:/upload.txt/")); producer.send(message);
Use an input stream:
// lets use a stream InputStream in = ...;
BlobMessage message = session.createBlobMessage(in); producer.send(message);
Receiving BLOB messages – BLOB message is a regular JMS message, so it can be received just like any other message. This example shows how to receive BLOB messages:
public class MyListener implements MessageListener { public void onMessage(Message message) { if (message instanceof BlobMessage) { BlobMessage blobMessage = (BlobMessage) message; InputStream in = blobMessage.getInputStream(); // process the stream... } } }
This example shows how to write a client application to send and receive BLOB messages:
String CTXFACTORY = "com.sybase.jms.client.InitialContextFactory"; Context context = null; String url = "iiop://" + hostname + ":" + port; Properties prop = new Properties(); prop.put(Context.INITIAL_CONTEXT_FACTORY, CTXFACTORY); prop.put(javax.naming.Context.PROVIDER_URL, url); prop.put(javax.naming.Context.SECURITY_PRINCIPAL, username); prop.put(javax.naming.Context.SECURITY_CREDENTIALS, password); context = new InitialContext(prop); String queueName = "testQueue";
QueueConnectionFactory factory = (QueueConnectionFactory) context .lookup("javax.jms.QueueConnectionFactory");
QueueConnection connection = factory.createQueueConnection (username, password);
((SybConnection)connection).setRemoteUploadURL("http://hostname:8000/ fileserver/"); QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); // Create a message producer and consumer. Queue queue = session.createQueue(queueName); QueueSender sender = session.createSender(queue); QueueReceiver receiver = null; receiver = session.createReceiver(queue); connection.start();
// Prepare a text message to send http://syberspace.sybase.com/ BlobMessage message =((SybSession) session).createBlobMessage(new File("d:/apachem.txt")); // Send the Msg sender.send(message); sender.close(); // Receive the message. Wait until msg arrives. if (receiver != null) { Message msg = receiver.receive(); BlobMessage blobmsg= (BlobMessage)msg; InputStream input= blobmsg.getInputStream(); //process the iputstream … receiver.close(); } if (connection != null) { connection.close(); }