Search in sources :

Example 1 with ByteArrayInputStream

use of org.fusesource.hawtbuf.ByteArrayInputStream in project spring-boot by spring-projects.

the class HeapdumpMvcEndpointTests method invokeShouldReturnGzipContent.

@Test
public void invokeShouldReturnGzipContent() throws Exception {
    MvcResult result = this.mvc.perform(get("/heapdump")).andExpect(status().isOk()).andReturn();
    byte[] bytes = result.getResponse().getContentAsByteArray();
    GZIPInputStream stream = new GZIPInputStream(new ByteArrayInputStream(bytes));
    byte[] uncompressed = FileCopyUtils.copyToByteArray(stream);
    assertThat(uncompressed).isEqualTo("HEAPDUMP".getBytes());
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(org.fusesource.hawtbuf.ByteArrayInputStream) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with ByteArrayInputStream

use of org.fusesource.hawtbuf.ByteArrayInputStream in project vcell by virtualcell.

the class VCMessageJms method loadBlobFile.

/**
 * see Property jmsBlobMessageMinSize "vcell.jms.blobMessageMinSize"
 * see Property jmsBlobMessageTempDir "vcell.jms.blobMessageTempDir"
 * see class MessageProducerSessionJms
 *
 * 1) Message Producer serializes into byte[] and compares size with PropertyLoader.jmsBlobMessageMinSize.
 * 2) For Large Object Messages (> threshold bytes), MessageProducerSessionJms writes bytes to a local file (e.g. MyBlobMessageTempDir/BlobMessage2295645974283237270.data).
 * 3) Pass file name as message properties so that receiver can delete file when done.
 * 4) consumer-side VCMessage infrastructure receives BlobMessage and invokes VCMessageJms.loadBlobMessage()
 * 5) loadBlobMessage() reads object from stream and attempts to delete both original and broker files.
 * 6) consumer's message listener calls getObjectContent() not knowing if it was sent as a Blob or not.
 * 7) message consumer calls VCMessageJms.removeBlobFile() to clean up disk.
 */
public void loadBlobFile() {
    if (blobObject != null) {
        return;
    }
    if (jmsMessage instanceof ObjectMessage && propertyExists(BLOB_MESSAGE_PERSISTENCE_TYPE) && getStringProperty(BLOB_MESSAGE_PERSISTENCE_TYPE).equals(BLOB_MESSAGE_PERSISTENCE_TYPE_FILE)) {
        try {
            long t1 = System.currentTimeMillis();
            // 
            // read serialized object from inputStream (from Broker's data file)
            // 
            String blobFileName = jmsMessage.getStringProperty(BLOB_MESSAGE_FILE_NAME);
            // 
            // get directory to retrieve Message BLOBs
            // 
            File localBlobDir = new File(PropertyLoader.getRequiredProperty(PropertyLoader.jmsBlobMessageTempDir));
            File producerBlobDir = new File(getStringProperty(BLOB_MESSAGE_PRODUCER_TEMPDIR));
            blobFile = new File(localBlobDir, blobFileName);
            if (!blobFile.exists()) {
                blobFile = new File(producerBlobDir, blobFileName);
                if (!blobFile.exists()) {
                    throw new RuntimeException("Message BLOB file \"" + blobFileName + "\" not found local=\"" + localBlobDir + "\" or producer=\"" + producerBlobDir + "\"");
                }
            }
            FileInputStream fis = new FileInputStream(blobFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            ObjectInputStream ois = new ObjectInputStream(bis);
            blobObject = (Serializable) ois.readObject();
            ois.close();
            bis.close();
            fis.close();
            delegate.onTraceEvent("VCMessageJms.loadBlobFile(): size=" + jmsMessage.getIntProperty(BLOB_MESSAGE_OBJECT_SIZE) + ", type=" + jmsMessage.getStringProperty(BLOB_MESSAGE_OBJECT_TYPE) + ", elapsedTime = " + (System.currentTimeMillis() - t1) + " ms");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    if (jmsMessage instanceof ObjectMessage && propertyExists(BLOB_MESSAGE_PERSISTENCE_TYPE) && getStringProperty(BLOB_MESSAGE_PERSISTENCE_TYPE).equals(BLOB_MESSAGE_PERSISTENCE_TYPE_MONGODB)) {
        try {
            long t1 = System.currentTimeMillis();
            // 
            // read serialized object from inputStream (from Broker's data file)
            // 
            String mongo_objectid_hex = jmsMessage.getStringProperty(BLOB_MESSAGE_MONGODB_OBJECTID);
            blobObjectId = new ObjectId(mongo_objectid_hex);
            byte[] blob = VCMongoDbDriver.getInstance().getBLOB(blobObjectId);
            ByteArrayInputStream bis = new ByteArrayInputStream(blob);
            ObjectInputStream ois = new ObjectInputStream(bis);
            blobObject = (Serializable) ois.readObject();
            ois.close();
            bis.close();
            delegate.onTraceEvent("VCMessageJms.loadBlobFile(): size=" + jmsMessage.getIntProperty(BLOB_MESSAGE_OBJECT_SIZE) + ", type=" + jmsMessage.getStringProperty(BLOB_MESSAGE_OBJECT_TYPE) + ", elapsedTime = " + (System.currentTimeMillis() - t1) + " ms");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ObjectId(org.bson.types.ObjectId) ByteArrayInputStream(org.fusesource.hawtbuf.ByteArrayInputStream) ObjectMessage(javax.jms.ObjectMessage) File(java.io.File) FileInputStream(java.io.FileInputStream) JMSException(javax.jms.JMSException) MessagePropertyNotFoundException(cbit.vcell.message.MessagePropertyNotFoundException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ByteArrayInputStream (org.fusesource.hawtbuf.ByteArrayInputStream)2 MessagePropertyNotFoundException (cbit.vcell.message.MessagePropertyNotFoundException)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 JMSException (javax.jms.JMSException)1 ObjectMessage (javax.jms.ObjectMessage)1 ObjectId (org.bson.types.ObjectId)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1