Example usage for javax.jms Connection close

List of usage examples for javax.jms Connection close

Introduction

In this page you can find the example usage for javax.jms Connection close.

Prototype


void close() throws JMSException;

Source Link

Document

Closes the connection.

Usage

From source file:de.elomagic.carafile.server.bl.SeedBean.java

public void seedChunk(final InputStream inputStream, final String chunkId)
        throws IOException, GeneralSecurityException, JMSException {
    LOG.debug("Seeding chunk " + chunkId);
    Chunk chunk = chunkDAO.findByIdentifier(chunkId);

    if (chunk == null) {
        throw new FileNotFoundException(
                "Chunk id " + chunkId + " not found. File already registered at registry?");
    }/*from w  ww .  j a  va2 s  .co m*/

    byte[] buf = new byte[chunk.getFileMeta().getChunkSize()];
    int bufferSize;
    try (InputStream in = inputStream) {
        bufferSize = readBlock(in, buf);
    } catch (IOException ex) {
        throw ex;
    }

    // Check SHA-1
    String sha1 = Hex.encodeHexString(DigestUtils.sha1(Arrays.copyOf(buf, bufferSize)));
    if (!chunk.getHash().equalsIgnoreCase(sha1)) {
        throw new GeneralSecurityException(
                "Chunk SHA-1 validation failed (Expected " + chunk.getHash() + ", but is " + sha1 + ")");
    }

    repositoryBean.writeChunk(chunkId, buf, bufferSize);

    LOG.debug("Chunk id " + chunkId + " seed");

    // Initiate to register at tracker
    LOG.debug("Queue up new file for registration.");
    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer messageProducer = session.createProducer(chunkQueue);

    TextMessage message = session.createTextMessage(chunkId);
    messageProducer.send(message);
    connection.close();
}

From source file:sk.seges.test.jms.activemq.SimpleActiveMQQueueReceiveTest.java

@Test
public void testReceive() throws Exception {
    Connection connection = activeMQConnectionFactory.createConnection();
    connection.start();/*from w w w  . ja  va2 s .c  o  m*/
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(activeMQTestQueueA);

    Message rawMessage = consumer.receive(1000);
    assertTrue(rawMessage instanceof TextMessage);

    TextMessage message = (TextMessage) rawMessage;
    assertEquals("test text", message.getText());
    connection.close();
}

From source file:sk.seges.test.jms.activemq.SimpleActiveMQQueueSendReceiveTest.java

@Test
public void testReceive() throws Exception {
    Connection connection = activeMQConnectionFactory.createConnection();
    connection.start();/*from   w w  w .  j ava 2  s. c  o m*/
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(activeMQTestQueueA);
    Message rawMessage = consumer.receive();
    assertTrue(rawMessage instanceof TextMessage);

    TextMessage message = (TextMessage) rawMessage;
    assertEquals("test text", message.getText());
    connection.close();
}

From source file:de.elomagic.carafile.server.bl.SeedBean.java

public MetaData seedFile(final InputStream inputStream, final String filename)
        throws IOException, GeneralSecurityException, JMSException {
    LOG.debug("Seeding file " + filename);
    MetaData md;/*from w  w w .  j a v  a  2 s  .  com*/
    md = new MetaData();
    md.setFilename(filename);
    md.setCreationDate(new Date());
    md.setChunkSize(DEFAULT_PIECE_SIZE);
    md.setRegistryURI(UriBuilder.fromUri(registryURI).build());

    MessageDigest messageDigest = DigestUtils.getSha1Digest();
    long totalBytes = 0;

    try (DigestInputStream dis = new DigestInputStream(inputStream, messageDigest)) {
        byte[] buffer = new byte[md.getChunkSize()];
        int bytesRead;
        while ((bytesRead = readBlock(dis, buffer)) > 0) {
            totalBytes += bytesRead;
            String chunkId = Hex
                    .encodeHexString(DigestUtils.sha1(new ByteArrayInputStream(buffer, 0, bytesRead)));

            repositoryBean.writeChunk(chunkId, buffer, bytesRead);

            URI uri = UriBuilder.fromUri(ownURI).build();

            ChunkData chunkData = new ChunkData(chunkId, uri);
            md.addChunk(chunkData);
        }
    }

    md.setSize(totalBytes);
    md.setId(Hex.encodeHexString(messageDigest.digest()));

    LOG.debug("File id of seed file is " + md.getId() + "; Size=" + md.getSize() + "; Chunks="
            + md.getChunks().size());

    // Initiate to register at tracker
    LOG.debug("Queue up new file for registration.");
    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer messageProducer = session.createProducer(fileQueue);

    ObjectMessage message = session.createObjectMessage(md);

    messageProducer.send(message);

    connection.close();

    return md;
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.jms.factory.JMSConnectionFactory.java

public boolean closeConnection(Connection connection) {
    try {//from   ww  w. j a  v a  2 s. c o  m
        connection.close();
        return true;
    } catch (JMSException e) {
        logger.error("JMS Exception while closing the connection.");
    }

    return false;
}

From source file:org.apache.falcon.messaging.FeedProducerTest.java

private void consumer() throws JMSException {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
    Connection connection = connectionFactory.createConnection();
    connection.start();/*w w w . ja v  a2 s  .c  o m*/

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createTopic(TOPIC_NAME);
    MessageConsumer consumer = session.createConsumer(destination);

    latch.countDown();
    verifyMesssage(consumer);

    connection.close();
}

From source file:org.apache.qpid.disttest.jms.ClientJmsDelegate.java

public void tearDownTest() {
    StringBuilder jmsErrorMessages = new StringBuilder();
    int failureCounter = 0;

    for (String subscription : _testSubscriptions.keySet()) {
        Session session = _testSubscriptions.get(subscription);
        try {//from w  ww  .  j  av a  2 s .  c  o  m
            session.unsubscribe(subscription);
        } catch (JMSException e) {
            LOGGER.error("Failed to unsubscribe '" + subscription + "' :" + e.getLocalizedMessage(), e);
            failureCounter++;
            appendErrorMessage(jmsErrorMessages, e);
        }
    }

    for (Map.Entry<String, Connection> entry : _testConnections.entrySet()) {
        Connection connection = entry.getValue();
        try {
            connection.close();
        } catch (JMSException e) {
            LOGGER.error("Failed to close connection '" + entry.getKey() + "' :" + e.getLocalizedMessage(), e);
            failureCounter++;
            appendErrorMessage(jmsErrorMessages, e);
        }
    }

    _testConnections.clear();
    _testSubscriptions.clear();
    _testSessions.clear();
    _testProducers.clear();
    _testConsumers.clear();

    if (failureCounter > 0) {
        throw new DistributedTestException("Tear down test encountered " + failureCounter
                + " failures with the following errors: " + jmsErrorMessages.toString());
    }
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.jms.HermesJmsRequestTransport.java

protected void closeSessionAndConnection(Connection connection, Session session) throws JMSException {
    if (session != null)
        session.close();//from  w w  w. j  ava 2 s .  com
    if (connection != null)
        connection.close();
}

From source file:com.linagora.obm.sync.TestQueueManager.java

@Test
public void testDurableSubscription() throws Exception {
    String testText = "test text";
    String clientId = "c1";

    Connection connection = queueManager.createConnection();
    connection.setClientID(clientId);//from   ww w .  j a  va2 s .com
    connection.start();
    Session consumerSession = queueManager.createSession(connection);
    MessageConsumer consumer = queueManager.createDurableConsumerOnTopic(consumerSession, TOPIC, clientId);
    consumerSession.close();
    connection.close();

    writeMessageOnTopic(testText, TOPIC, queueManager);

    Connection connection2 = createManagedConnection();
    connection2.setClientID(clientId);
    connection2.start();

    consumerSession = queueManager.createSession(connection2);
    consumer = queueManager.createDurableConsumerOnTopic(consumerSession, TOPIC, clientId);

    TextMessage messageReceived = (TextMessage) consumer.receive(TIMEOUT);
    Assert.assertEquals(testText, messageReceived.getText());
}

From source file:org.apache.activemq.camel.JmsJdbcXARollbackTest.java

private void sendJMSMessageToKickOffRoute() throws Exception {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://testXA");
    factory.setWatchTopicAdvisories(false);
    Connection connection = factory.createConnection();
    connection.start();//from  w  w  w.ja va2  s .  c om
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(new ActiveMQQueue("scp_transacted"));
    TextMessage message = session.createTextMessage("Some Text, messageCount:" + messageCount++);
    message.setJMSCorrelationID("pleaseCorrelate");
    producer.send(message);
    connection.close();
}