activemq-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mike_pone <michael.a.p...@lmco.com>
Subject problems setting username and password on the ActiveMQComponent using Java
Date Wed, 10 Jul 2013 15:16:33 GMT
I need to manually create an authenticated connection to an activemq broker
using only Java.  Setting the username and password on the ActiveMQComponent
does not seem to be working in Java as it does in spring.  I have a simple
test program that shows a spring configured ActiveMQComponent and a Java
Configured ActiveMQComponent.  Both are connecting to an embedded broker
with the same credentials.  The Spring component will work fine and let the
message go through, but the Java configured component throws the following
exception, " java.lang.SecurityException: User name [null] or password is
invalid."

public class TestApp { 

    @Produce(uri = "direct:test")
    protected ProducerTemplate template;
    @Produce(uri = "direct:test2")
    protected ProducerTemplate template2;

    private static final Log log = LogFactory.getLog(TestApp.class);
    public static void main(final String[] args) throws Exception {

        log.info("Initializing Spring");
        final ClassPathXmlApplicationContext springContext = new
ClassPathXmlApplicationContext("spring-context.xml");
        springContext.registerShutdownHook();
        final CamelContext camelContext =
springContext.getBean("camelContext", CamelContext.class);
        final TestApp app =springContext.getBean("testApp", TestApp.class);
        
        String brokerUrl = brokerUrl = String.format("%s://%s:%d", "tcp",
"localhost", 61616);
        final ActiveMQComponent component =
ActiveMQComponent.activeMQComponent(brokerUrl);
        component.setUserName("sa");
        component.setPassword("manager"); 
        
        ActiveMQConfiguration config = (ActiveMQConfiguration)
component.getConfiguration();
        log.info("username : " + config.getUserName());
        log.info("username : " + config.getPassword());
        camelContext.addComponent("activemq2", component);

        //simply creates a route from the direct parameter to the activemq
topic using the passed in activemq prefix.
        camelContext.addRoutes(new TestRouteBuilder("activemq",
"direct:test", "topic:test")) ;
        camelContext.addRoutes(new TestRouteBuilder("activemq2",
"direct:test2", "topic:test2")) ;
        camelContext.start();
    	
    	app.start();
    }
    
    private void start() throws Exception{
        final String message = new String("hello");

        log.info("sending first message.");
        template.sendBody(message); //works
        log.info("first message sent.");
        log.info("sending second message.");
        template2.sendBody(message); //throws Exception
        log.info("second message sent.");
        
    }
}

Here is my spring config :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring
       http://camel.apache.org/schema/spring/camel-spring.xsd">
 
    <camelContext xmlns="http://camel.apache.org/schema/spring"
        xmlns:tfm="http://tfm.faa.gov/tfms/TFMS_XIS" id="camelContext"
        autoStartup="false" trace="false">
         
    </camelContext>
    
    <bean id="testApp" class="com.mike.test.TestApp"/>
 
     <bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
	    <property name="config" value="classpath:activemq/activemq-broker.xml"
/>
	    <property name="start" value="true" />
	</bean>
      
      <bean id="activemq"
class="org.apache.activemq.camel.component.ActiveMQComponent" >
        <property name="connectionFactory">
          <bean class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="tcp://localhost:61616" />
            <property name="userName" value="sa"/>
            <property name="password" value="manager"/>
          </bean>
        </property>
    </bean>
     
</beans>

Here is the config for the embedded broker.  I've also tried this with a
standalone broker instance and see the same issue : 

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
 

	<broker xmlns="http://activemq.apache.org/schema/core"
		brokerName="broker1" dataDirectory="target/activemq-data" start="false">
 
		<destinationPolicy>
			<policyMap>
				<policyEntries>
					<policyEntry topic=">" producerFlowControl="true"
						memoryLimit="1mb">
						<pendingSubscriberPolicy>
							<vmCursor />
						</pendingSubscriberPolicy>
					</policyEntry>
					<policyEntry queue=">" producerFlowControl="true"
						memoryLimit="1mb">
					</policyEntry>
				</policyEntries>
			</policyMap>
		</destinationPolicy>

		<managementContext>
			<managementContext createConnector="false" />
		</managementContext>

		<persistenceAdapter>
			<kahaDB directory="target/activemq-data/kahadb" />
		</persistenceAdapter>

		<transportConnectors>
			<transportConnector name="openwire" uri="tcp://0.0.0.0:61616" />
		</transportConnectors>
		<plugins>
			<simpleAuthenticationPlugin>
				<users>
					<authenticationUser username="sa" password="manager"
						groups="producers,consumers,admins" /> 
						
				</users>
			</simpleAuthenticationPlugin>
			<authorizationPlugin>
				<map>
					<authorizationMap>
						<authorizationEntries>
							<authorizationEntry queue=">" write="producers,admins"
								read="consumers,admins" admin="admins" />
							<authorizationEntry topic=">" write="producers,admins"
								read="consumers,admins" admin="admins" />
							<authorizationEntry topic="ActiveMQ.Advisory.>" 
								read="admins,publishers,consumers" 
								write="admins,publishers,consumers" 
								admin="admins,publishers,consumers"/> 
								
						</authorizationEntries>
					</authorizationMap>
				</map>
			</authorizationPlugin>
		</plugins>
	</broker>

</beans>

Here is the TestRouteBuilder for completeness sake :

public final class TestRouteBuilder extends RouteBuilder { 

    private final String activeMqPrefix; 
    private final String from;
    private final String to;
    
    public TestRouteBuilder(final String activeMqPrefix, String from, String
to) {
        this.activeMqPrefix = activeMqPrefix;
        this.from=from;
        this.to=to;
    } 
    
    @Override
    public void configure() throws Exception {
    	from(from).to(activeMqPrefix +":" + to);
    }
}




--
View this message in context: http://activemq.2283324.n4.nabble.com/problems-setting-username-and-password-on-the-ActiveMQComponent-using-Java-tp4669091.html
Sent from the ActiveMQ - Dev mailing list archive at Nabble.com.

Mime
View raw message