Search in sources :

Example 1 with CLIException

use of org.apache.activemq.artemis.cli.CLIException in project activemq-artemis by apache.

the class Create method write.

private void write(String source, File target, HashMap<String, String> filters, boolean unixTarget) throws Exception {
    if (target.exists() && !force) {
        throw new CLIException(String.format("The file '%s' already exists.  Use --force to overwrite.", target));
    }
    String content = readTextFile(source, filters);
    // and then writing out in the new target encoding..  Let's also replace \n with the values
    // that is correct for the current platform.
    String separator = unixTarget && IS_CYGWIN ? "\n" : System.getProperty("line.separator");
    content = content.replaceAll("\\r?\\n", Matcher.quoteReplacement(separator));
    ByteArrayInputStream in = new ByteArrayInputStream(content.getBytes(encoding));
    try (FileOutputStream fout = new FileOutputStream(target)) {
        copy(in, fout);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) CLIException(org.apache.activemq.artemis.cli.CLIException)

Example 2 with CLIException

use of org.apache.activemq.artemis.cli.CLIException in project activemq-artemis by apache.

the class LockAbstract method lockCLI.

void lockCLI(File lockPlace) throws Exception {
    if (lockPlace != null) {
        lockPlace.mkdirs();
        if (serverLockFile == null) {
            File fileLock = new File(lockPlace, "cli.lock");
            serverLockFile = new RandomAccessFile(fileLock, "rw");
        }
        try {
            FileLock lock = serverLockFile.getChannel().tryLock();
            if (lock == null) {
                throw new CLIException("Error: There is another process using the server at " + lockPlace + ". Cannot start the process!");
            }
            serverLockLock = lock;
        } catch (OverlappingFileLockException e) {
            throw new CLIException("Error: There is another process using the server at " + lockPlace + ". Cannot start the process!");
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileLock(java.nio.channels.FileLock) CLIException(org.apache.activemq.artemis.cli.CLIException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 3 with CLIException

use of org.apache.activemq.artemis.cli.CLIException in project activemq-artemis by apache.

the class ArtemisTest method testSimpleRun.

public void testSimpleRun(String folderName) throws Exception {
    File instanceFolder = temporaryFolder.newFolder(folderName);
    setupAuth(instanceFolder);
    String queues = "q1,q2";
    String addresses = "a1,a2";
    // This is usually set when run from the command line via artemis.profile
    Run.setEmbedded(true);
    Artemis.main("create", instanceFolder.getAbsolutePath(), "--force", "--silent", "--no-web", "--queues", queues, "--addresses", addresses, "--no-autotune", "--require-login");
    System.setProperty("artemis.instance", instanceFolder.getAbsolutePath());
    try {
        // Some exceptions may happen on the initialization, but they should be ok on start the basic core protocol
        Artemis.internalExecute("run");
        try (ServerLocator locator = ServerLocatorImpl.newLocator("tcp://localhost:61616");
            ClientSessionFactory factory = locator.createSessionFactory();
            ClientSession coreSession = factory.createSession("admin", "admin", false, true, true, false, 0)) {
            for (String str : queues.split(",")) {
                ClientSession.QueueQuery queryResult = coreSession.queueQuery(SimpleString.toSimpleString(str));
                assertTrue("Couldn't find queue " + str, queryResult.isExists());
            }
            for (String str : addresses.split(",")) {
                ClientSession.AddressQuery queryResult = coreSession.addressQuery(SimpleString.toSimpleString(str));
                assertTrue("Couldn't find address " + str, queryResult.isExists());
            }
        }
        try {
            Artemis.internalExecute("data", "print");
            Assert.fail("Exception expected");
        } catch (CLIException expected) {
        }
        Artemis.internalExecute("data", "print", "--f");
        assertEquals(Integer.valueOf(100), Artemis.internalExecute("producer", "--message-count", "100", "--user", "admin", "--password", "admin"));
        assertEquals(Integer.valueOf(100), Artemis.internalExecute("consumer", "--break-on-null", "--receive-timeout", "100", "--user", "admin", "--password", "admin"));
        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = cf.createConnection("admin", "admin");
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        MessageProducer producer = session.createProducer(ActiveMQDestination.createDestination("queue://TEST", ActiveMQDestination.TYPE.QUEUE));
        TextMessage message = session.createTextMessage("Banana");
        message.setStringProperty("fruit", "banana");
        producer.send(message);
        for (int i = 0; i < 100; i++) {
            message = session.createTextMessage("orange");
            message.setStringProperty("fruit", "orange");
            producer.send(message);
        }
        session.commit();
        connection.close();
        cf.close();
        assertEquals(Integer.valueOf(1), Artemis.internalExecute("browser", "--txt-size", "50", "--filter", "fruit='banana'", "--user", "admin", "--password", "admin"));
        assertEquals(Integer.valueOf(100), Artemis.internalExecute("browser", "--txt-size", "50", "--filter", "fruit='orange'", "--user", "admin", "--password", "admin"));
        assertEquals(Integer.valueOf(101), Artemis.internalExecute("browser", "--txt-size", "50", "--user", "admin", "--password", "admin"));
        // should only receive 10 messages on browse as I'm setting messageCount=10
        assertEquals(Integer.valueOf(10), Artemis.internalExecute("browser", "--txt-size", "50", "--message-count", "10", "--user", "admin", "--password", "admin"));
        // Nothing was consumed until here as it was only browsing, check it's receiving again
        assertEquals(Integer.valueOf(1), Artemis.internalExecute("consumer", "--txt-size", "50", "--break-on-null", "--receive-timeout", "100", "--filter", "fruit='banana'", "--user", "admin", "--password", "admin"));
        // Checking it was acked before
        assertEquals(Integer.valueOf(100), Artemis.internalExecute("consumer", "--txt-size", "50", "--break-on-null", "--receive-timeout", "100", "--user", "admin", "--password", "admin"));
    } finally {
        stopServer();
    }
}
Also used : Connection(javax.jms.Connection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) CLIException(org.apache.activemq.artemis.cli.CLIException) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) MessageProducer(javax.jms.MessageProducer) File(java.io.File) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession)

Aggregations

CLIException (org.apache.activemq.artemis.cli.CLIException)3 File (java.io.File)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 FileLock (java.nio.channels.FileLock)1 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)1 Connection (javax.jms.Connection)1 MessageProducer (javax.jms.MessageProducer)1 Session (javax.jms.Session)1 TextMessage (javax.jms.TextMessage)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)1 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)1 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)1 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)1