title: How to connect to an ActiveMQ server by setting the username and password programmatically?
body: <p>I create an ActiveMQ server in order to send some messages. I use Apache Activemq version 5.14.4
  I want to be asked for username and password when I try to connect to the ActiveMQ server and that's why I added a simple authentification plugin into the activemq.xml as you can see bellow.</p>
  
  <pre><code>String username = "admin";
  String password = "anything";
  ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
          factory.setUserName(username);
          factory.setPassword(password);
          factory.setTrustedPackages(Arrays.asList("com.h4p.server"));
          Connection connection = null;
          Session session = null;
          Destination destination = null;
          try {
              connection = factory.createConnection();
              connection.start();
              session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
              destination = session.createQueue("myQueue");
          } catch (JMSException e) {
              e.printStackTrace();
          }
  ...
  </code></pre>
  
  <p>Into the broker element I added a security level using SimpleAuthentificationPlugin:</p>
  
  <pre><code>    &lt;simpleAuthenticationPlugin anonymousAccessAllowed="false"&gt;
          &lt;users&gt;
              &lt;authenticationUser username="admin" password="anything" groups="admins"/&gt;
          &lt;/users&gt;
      &lt;/simpleAuthenticationPlugin&gt;
  
      &lt;authorizationPlugin&gt;
          &lt;map&gt;
              &lt;authorizationMap&gt;
                  &lt;authorizationEntries&gt;
                      &lt;authorizationEntry queue="myQueue" write="admins" read="admins" admin="admins" /&gt;  
                      &lt;authorizationEntry topic="ActiveMQ.Advisory.&gt;" write="admins" read="admins" admin="admins" /&gt;                      
                  &lt;/authorizationEntries&gt;
              &lt;/authorizationMap&gt;
          &lt;/map&gt;
      &lt;/authorizationPlugin&gt;I
  </code></pre>
  
  <p>As you can see, I set the username and password at factory level, although I can set them when I create the connection. </p>
  
  <p>Which option is best, to set the username&amp;password to the factory or when I create the connection?</p>
  
  <pre><code>ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
          factory.setUserName(username);
          factory.setPassword(password);
  </code></pre>
  
  <p>vs</p>
  
  <pre><code>connection = factory.createConnection(username, password);
  </code></pre>
  
  <p>Anyway, with security I can't to connect to the ActiveMQ server:</p>
  
  <pre><code>javax.jms.JMSException: Could not connect to broker URL: tcp://localhost:61616. Reason: java.net.ConnectException: Connection refused: connect
  </code></pre>
  
  <p>The files in \apache-activemq-5.14.4\conf directory have this configurations:
  users.properties: <code>admin=admin</code>, groups.properties: <code>admins=admin</code> and credentials.properties: </p>
  
  <pre><code>activemq.username=admin
  activemq.password=anything
  </code></pre>
  
  <p>If I comment the simpleAuthenticationPlugin and authorizationPlugin, then I can connect to the server, send the messages I want to sent and consume them. Also, I can connect to the server,send and consume the messages if I remove <code>anonymousAccessAllowed="false"</code> or put it on true, but this is not what I want. </p>
  
  <p><strong>Could you please tell me how can I connect to the ActiveMQ server configured with that security level by setting the username and password programmatically?</strong></p>