/* * Copyright 2009 Red Hat, Inc. * Red Hat licenses this file to you under the Apache License, version * 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. */ package org.hornetq.javaee.example; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import javax.jms.BytesMessage; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.naming.Context; import javax.naming.InitialContext; /** * An example which sends a message to a source queue and consume from a target queue. * The source and target queues are bridged by a JMS Bridge configured and running in JBoss AS 5. * * @author Jeff Mesnil */ public class JMSBridgeExample { /** * The message we will send is size 10GiB, even though we are only running in 50MB of RAM on both * client and server. *

* This may take some considerable time to create, send and consume - if it takes too long or you * don't have enough disk space just reduce the file size here */ private static final long FILE_SIZE = 2L * 1024 * 1024 * 1024; // 2 GB message // private static final long FILE_SIZE = 1024L * 1024 * 1024; // 1 GB message (klapppt) // private static final long FILE_SIZE = 100L * 1024 * 1024; // 100 MB message (klapppt) // private static final long FILE_SIZE = 3L * 1024 * 1024; // 3 MB message (klapppt) public static void main(final String[] args) throws Exception { InitialContext initialContext = null; Connection sourceConnection = null; Connection targetConnection = null; try { // Step 1. Obtain an Initial Context final Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); env.put(Context.PROVIDER_URL, "remote://localhost:4447"); env.put(Context.SECURITY_PRINCIPAL, "guest"); env.put(Context.SECURITY_CREDENTIALS, "password"); initialContext = new InitialContext(env); // Step 2. Lookup the JMS connection factory ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/jms/RemoteConnectionFactory"); // Step 3. Lookup the source queue Queue sourceQueue = (Queue)initialContext.lookup("jms/queues/sourceQueue"); // Step 4. Create a connection, a session and a message producer for the *source* queue sourceConnection = cf.createConnection("guest", "password"); Session sourceSession = sourceConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer sourceProducer = sourceSession.createProducer(sourceQueue); // Step 5. Create a huge file - this will form the body of the message we will send. System.out.println("Creating a file to send of size " + FILE_SIZE + " bytes. This may take a little while... " + "If this is too big for your disk you can easily change the FILE_SIZE in the example."); File fileInput = new File("huge_message_to_send.dat"); createFile(fileInput, FILE_SIZE); System.out.println("File created."); // Step 6. Create a BytesMessage BytesMessage message = sourceSession.createBytesMessage(); // Step 7. We set the InputStream on the message. When sending the message will read the InputStream // until it gets EOF. In this case we point the InputStream at a file on disk, and it will suck up the entire // file, however we could use any InputStream not just a FileInputStream. FileInputStream fileInputStream = new FileInputStream(fileInput); //BufferedInputStream bufferedInput = new BufferedInputStream(fileInputStream); message.setObjectProperty("JMS_HQ_InputStream", fileInputStream); System.out.println("Sending the huge message."); // Step 8. Send the Message sourceProducer.send(message); System.out.println("Large Message sent"); // Step 9. Close the *source* connection sourceConnection.close(); // Step 7. Lookup the *target* queue Queue targetQueue = (Queue)initialContext.lookup("jms/queues/targetQueue"); // Step 8. Create a connection, a session and a message consumer for the *target* queue targetConnection = cf.createConnection("guest", "password"); Session targetSession = targetConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer targetConsumer = targetSession.createConsumer(targetQueue); // Step 9. Start the connection to receive messages from the *targe* queue targetConnection.start(); System.out.println("Receiving message."); // Step 10. Receive the message. When we receive the large message we initially just receive the message with // an empty body. BytesMessage messageReceived = (BytesMessage)targetConsumer.receive(120000); System.out.println("Received message with: " + messageReceived.getLongProperty("_HQ_LARGE_SIZE") + " bytes. Now streaming to file on disk."); // Step 11. We set an OutputStream on the message. This causes the message body to be written to the // OutputStream until there are no more bytes to be written. // You don't have to use a FileOutputStream, you can use any OutputStream. // You may choose to use the regular BytesMessage or // StreamMessage interface but this method is much faster for large messages. File outputFile = new File("huge_message_received.dat"); FileOutputStream fileOutputStream = new FileOutputStream(outputFile); //BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutputStream); // Step 12. This will save the stream and wait until the entire message is written before continuing. messageReceived.setObjectProperty("JMS_HQ_SaveStream", fileOutputStream); // Step 12(alternative). This won't wait the stream to finish. You need to keep the consumer active. // messageReceived.setObjectProperty("JMS_HQ_OutputStream", bufferedOutput); fileOutputStream.close(); System.out.println("File streamed to disk. Size of received file on disk is " + outputFile.length()); } finally { // Step 13. Be sure to close the resources! if (initialContext != null) { initialContext.close(); } if (sourceConnection != null) { sourceConnection.close(); } if (targetConnection != null) { targetConnection.close(); } } } /** * @param file * @param fileSize * @throws FileNotFoundException * @throws IOException */ private static void createFile(final File file, final long fileSize) throws IOException { FileOutputStream fileOut = new FileOutputStream(file); BufferedOutputStream buffOut = new BufferedOutputStream(fileOut); byte[] outBuffer = new byte[1024 * 1024]; for (long i = 0; i < fileSize; i += outBuffer.length) { buffOut.write(outBuffer); } buffOut.close(); } }