[ 
https://issues.apache.org/jira/browse/ARTEMIS-4372?focusedWorklogId=873936&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-873936
 ]

ASF GitHub Bot logged work on ARTEMIS-4372:
-------------------------------------------

                Author: ASF GitHub Bot
            Created on: 01/Aug/23 02:15
            Start Date: 01/Aug/23 02:15
    Worklog Time Spent: 10m 
      Work Description: clebertsuconic commented on code in PR #4565:
URL: https://github.com/apache/activemq-artemis/pull/4565#discussion_r1280044335


##########
artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java:
##########
@@ -157,68 +174,132 @@ private ConnectionFactory 
createAMQPConnectionFactory(String brokerURL,
       }
 
       try {
-         Connection connection = cf.createConnection();
-         connection.close();
+         tryConnect(brokerURL, user, password, cf);
          return cf;
       } catch (JMSSecurityException e) {
          // if a security exception will get the user and password through an 
input
          getActionContext().err.println("Connection failed::" + 
e.getMessage());
-         cf = new JmsConnectionFactory(inputUser(user), 
inputPassword(password), brokerURL);
+         user = inputUser(user);
+         password = inputPassword(password);
+         cf = new JmsConnectionFactory(user, password, brokerURL);
          if (clientID != null) {
             cf.setClientID(clientID);
          }
+         try {
+            tryConnect(brokerURL, user, password, cf);
+         } catch (Exception e2) {
+            e.printStackTrace();
+         }
          return cf;
       } catch (JMSException e) {
          // if a connection exception will ask for the URL, user and password
          getActionContext().err.println("Connection failed::" + 
e.getMessage());
-         cf = new JmsConnectionFactory(inputUser(user), 
inputPassword(password), inputBrokerURL(brokerURL));
+         brokerURL = inputBrokerURL(brokerURL);
+         user = inputUser(user);
+         password = inputPassword(password);
+         cf = new JmsConnectionFactory(user, password, brokerURL);
          if (clientID != null) {
             cf.setClientID(clientID);
          }
+         try {
+            tryConnect(brokerURL, user, password, cf);
+         } catch (Exception e2) {
+            e2.printStackTrace();
+         }
          return cf;
       }
    }
 
    protected ActiveMQConnectionFactory createCoreConnectionFactory() {
+      recoverConnectionInformation();
       return createCoreConnectionFactory(brokerURL, user, password, clientID);
    }
 
+   private void recoverConnectionInformation() {
+      if (CONNECTION_INFORMATION.get() != null) {
+         ConnectionInformation connectionInfo = CONNECTION_INFORMATION.get();
+         if (this.user == null) {
+            this.user  = connectionInfo.user;
+         }
+         if (this.password == null) {
+            this.password  = connectionInfo.password;
+         }
+         if (this.brokerURL == null) {
+            this.brokerURL  = connectionInfo.uri;
+         }
+
+      }
+   }
+
+   void saveConnectionInfo(String brokerURL, String user, String password) {
+      if (Shell.inShell() && CONNECTION_INFORMATION.get() == null) {
+         CONNECTION_INFORMATION.set(new ConnectionInformation(brokerURL, user, 
password));
+         System.out.println("CLI connected to broker " + brokerURL + ", user:" 
+ user);
+      }
+   }
+
    protected ActiveMQConnectionFactory createCoreConnectionFactory(String 
brokerURL,
                                                                    String user,
                                                                    String 
password,
                                                                    String 
clientID) {
+      if (brokerURL.startsWith("amqp://")) {
+         // replacing amqp:// by tcp://
+         brokerURL = "tcp" + brokerURL.substring(4);
+      }
+
       ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerURL, 
user, password);
       if (clientID != null) {
          getActionContext().out.println("Consumer:: clientID = " + clientID);
          cf.setClientID(clientID);
       }
       try {
-         Connection connection = cf.createConnection();
-         connection.close();
+         tryConnect(brokerURL, user, password, cf);
          return cf;
       } catch (JMSSecurityException e) {
          // if a security exception will get the user and password through an 
input
          if (getActionContext() != null) {
             getActionContext().err.println("Connection failed::" + 
e.getMessage());
          }
-         cf = new ActiveMQConnectionFactory(brokerURL, inputUser(user), 
inputPassword(password));
+         user = inputUser(user);
+         password = inputPassword(password);
+         cf = new ActiveMQConnectionFactory(brokerURL, user, password);
          if (clientID != null) {
             cf.setClientID(clientID);
          }
+         try {
+            tryConnect(brokerURL, user, password, cf);
+         } catch (Exception e2) {
+         }
          return cf;
       } catch (JMSException e) {
          // if a connection exception will ask for the URL, user and password
          if (getActionContext() != null) {
             getActionContext().err.println("Connection failed::" + 
e.getMessage());
          }
-         cf = new ActiveMQConnectionFactory(inputBrokerURL(brokerURL), 
inputUser(user), inputPassword(password));
+         brokerURL = inputBrokerURL(brokerURL);
+         user = inputUser(user);
+         password = inputPassword(password);
+         cf = new ActiveMQConnectionFactory(brokerURL, user, password);
          if (clientID != null) {
             cf.setClientID(clientID);
          }
+         try {
+            tryConnect(brokerURL, user, password, cf);
+         } catch (Exception e2) {
+         }

Review Comment:
   I see what you mean now.





Issue Time Tracking
-------------------

    Worklog Id:     (was: 873936)
    Time Spent: 6h 40m  (was: 6.5h)

> Move CLI framework to picocli and implement auto-complete
> ---------------------------------------------------------
>
>                 Key: ARTEMIS-4372
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-4372
>             Project: ActiveMQ Artemis
>          Issue Type: New Feature
>            Reporter: Clebert Suconic
>            Assignee: Clebert Suconic
>            Priority: Major
>             Fix For: 2.31.0
>
>          Time Spent: 6h 40m
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to