You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by Raffaele <r....@prismasw.it> on 2007/06/08 11:22:02 UTC

Embedded Broker - configuration via Java

Hi all,

I'm trying to create, configure and launch a broker all in one class.

I have a ClassNotFoundException executing the following lines:

BrokerService broker = new BrokerService();
broker.addConnector("tcp://localhost:61616");
broker.start();

And the exception thrown by addConnector(...) is this:

java.lang.NoClassDefFoundError:
javax/management/MalformedObjectNameException
	at
org.apache.activemq.broker.BrokerService.getManagementContext(BrokerService.java:696)
	at
org.apache.activemq.broker.BrokerService.createRegionBroker(BrokerService.java:1265)
	at
org.apache.activemq.broker.BrokerService.createBroker(BrokerService.java:1209)
	at
org.apache.activemq.broker.BrokerService.getBroker(BrokerService.java:508)
	at
org.apache.activemq.broker.BrokerService.addConnector(BrokerService.java:163)
	at
org.apache.activemq.broker.BrokerService.addConnector(BrokerService.java:153)
	at pss.jms.EmbeddedBroker.main(EmbeddedBroker.java:49)

In my simple test project I'm including only the apache-activemq-4.*.jar
found in the activemq home directory.
1) Which other jar should I include?
2) Is there a page on your confluence where it is explained how to configure
a broker ALL using only Java without any xml and without Spring?

Thanks, best regards.

Raffaele



-- 
View this message in context: http://www.nabble.com/Embedded-Broker---configuration-via-Java-tf3888599s2354.html#a11023134
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Embedded Broker - configuration via Java

Posted by Raffaele <r....@prismasw.it>.

Thanks, I've found the answer myself...

However, the problem in my code was simply that THE PERSISTENCE
CONFIGURATION MUST BE DONE BEFORE ADDING THE CONNECTOR.

That is, the call of setPersistence() method must be done before
m_broker.addConnector("tcp://localhost:61616");

Could I create a page on your Confluence explaining this simple BUT NOT
DOCUMENTED question? Providing also my very very simple example about how to
configure a BrokerService with persistence on MySql DB?
If the answer is yes, where can I append the page? I would put it in the FAQ
section...
Is there a test class about persistence configuration of a BrokerService? I
looked for it in test folder of activemq-core project but I didn't find it.

Best regards.

Raffaele

P.S.: Documentation should have the highest priority, especially in open
projects very useful like this.




Raffaele wrote:
> 
> I'm sorry but I need more help to configure from Java an embedded
> broker...
> 
> In particular I 'm not able to set persitence in MySql, in fact when I
> launch my example (that you can found after this message) I cannot see
> messages in tables...
> And, if I delete the three tables, my code don't reacreate them...
> 
> Javadoc of Brokerservice is poor and there isn't a page that explains the
> configurations steps from directly Java code...
> 
> In my example I start a broker and create a consumer that send messages to
> a queue...
> 
> The main question is "Why do the messages are stored on db"?
> The code is the following:
> import javax.jms.JMSException;
> 
> import org.apache.activemq.broker.BrokerService;
> import org.apache.activemq.memory.UsageManager;
> import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
> import org.apache.activemq.store.jdbc.adapter.MySqlJDBCAdapter;
> import org.apache.commons.dbcp.BasicDataSource;
> 
> 
> 
> import webrainbow.util.debug.WrCheck;
> 
> public class EmbeddedBroker {
> 	
> 	private BrokerService m_broker;
> 	
> 	public EmbeddedBroker() {
> 		m_broker = new BrokerService();
> 		
> 		// Some configurations
> 		m_broker.setBrokerName("EmbeddedBroker_localhost");
> 		m_broker.setUseJmx(false);
> 		m_broker.setPersistent(true);
> 
> 		// Setto il memoryManager
> 		UsageManager um = new UsageManager();
> 		// Setto il limite in byte
> 		um.setLimit(20000000);		
> 		m_broker.setMemoryManager(um);
> 		
> 		
> 		// Add connector
> 		try {
> 			m_broker.addConnector("tcp://localhost:61616");
> 		} catch (Exception e) {
> 			WrCheck.logError(e);
> 		}
> 	}
> 	
> 	
> 	public void go() {
> 		setPersistence();
> 				
> 		try {
> 			m_broker.start();
> 		} catch (Exception e) {
> 			WrCheck.logError(e);
> 		}		
> 		
> 		startDeliveryToQueue();
> 	}
> 	
> 	
> 	private void setPersistence() {
> 		// Create a JDBC persistence adapter and set it to use the Oracle
> Adapter.
> 		JDBCPersistenceAdapter jdbcPersistence = new JDBCPersistenceAdapter();
> 
> 		jdbcPersistence.setAdapter(new MySqlJDBCAdapter());
> 
> 		// Create a basic datasource (org.apache.commons.dbcp.BasicDataSource)
> and 
> 		// set the JDBC properties on it.
> 
> 		BasicDataSource bds = new BasicDataSource();
> 
> 		bds.setUrl("jdbc:mysql://localhost/activemq?relaxAutoCommit=true");
> 
> 		bds.setDriverClassName("com.mysql.jdbc.Driver");
> 
> 		bds.setUsername("root");
> 
> 		bds.setPassword("");
> 		
> 		bds.setPoolPreparedStatements(true);
> 
> 		// Set the datasource on the JDBC persistence adapter.
> 		jdbcPersistence.setDataSource(bds);
> 		
> 		m_broker.setPersistenceAdapter(jdbcPersistence);		
> 	}
> 	
> 	
> 	private void startDeliveryToQueue() {
> 		QueueProducer producer = null;
>         try {
>         	producer = new QueueProducer("tcp://localhost:61616",
> "ARCHIVER_QUEUE");
>         } catch (JMSException e1) {
>             WrCheck.logWarn(e1);            
>         } 
>         
>         for(;;) {
>             try {            	
>         		String time = String.valueOf(System.currentTimeMillis());                
>                 WrCheck.logInfo("Sending queue message --> <" + time +
> ">");                
>                 producer.sendMessage(time);
>             	
>             } catch (JMSException ex) {
>                 WrCheck.logWarn(ex);
>                                 
>                 try {
>                     WrCheck.logInfo("WebServerSimulator going to
> sleep...");
>                     Thread.sleep(10 * 1000);
>                     
>                     producer.restart();
>                 } 
>                 catch (Exception e) {
>                     WrCheck.logWarn(e);
>                 }
>             }            
>             catch (Exception exc) {
>                 WrCheck.logFatal(exc);                
>             }
>             
>             try {
>                 Thread.sleep((long) (Math.random() * 3000));
>             } catch (InterruptedException e) {
>                 WrCheck.logWarn(e);               
>             }
>         }
> 	}
> 	
> 	
> 	public static void main(String[] args) throws Exception {
> 		EmbeddedBroker eb = new EmbeddedBroker();
> 		eb.go();		
> 	}
> }
> 
> Best regards.
> 

-- 
View this message in context: http://www.nabble.com/Embedded-Broker---configuration-via-Java-tf3888599s2354.html#a11057175
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Embedded Broker - configuration via Java

Posted by Raffaele <r....@prismasw.it>.
I'm sorry but I need more help to configure from Java an embedded broker...

In particular I 'm not able to set persitence in MySql, in fact when I
launch my example (that you can found after this message) I cannot see
messages in tables...
And, if I delete the three tables, my code don't reacreate them...

Javadoc of Brokerservice is poor and there isn't a page that explains the
configurations steps from directly Java code...

In my example I start a broker and create a consumer that send messages to a
queue...

The main question is "Why do the messages are stored on db"?
The code is the following:
import javax.jms.JMSException;

import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.memory.UsageManager;
import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
import org.apache.activemq.store.jdbc.adapter.MySqlJDBCAdapter;
import org.apache.commons.dbcp.BasicDataSource;



import webrainbow.util.debug.WrCheck;

public class EmbeddedBroker {
	
	private BrokerService m_broker;
	
	public EmbeddedBroker() {
		m_broker = new BrokerService();
		
		// Some configurations
		m_broker.setBrokerName("EmbeddedBroker_localhost");
		m_broker.setUseJmx(false);
		m_broker.setPersistent(true);

		// Setto il memoryManager
		UsageManager um = new UsageManager();
		// Setto il limite in byte
		um.setLimit(20000000);		
		m_broker.setMemoryManager(um);
		
		
		// Add connector
		try {
			m_broker.addConnector("tcp://localhost:61616");
		} catch (Exception e) {
			WrCheck.logError(e);
		}
	}
	
	
	public void go() {
		setPersistence();
				
		try {
			m_broker.start();
		} catch (Exception e) {
			WrCheck.logError(e);
		}		
		
		startDeliveryToQueue();
	}
	
	
	private void setPersistence() {
		// Create a JDBC persistence adapter and set it to use the Oracle Adapter.
		JDBCPersistenceAdapter jdbcPersistence = new JDBCPersistenceAdapter();

		jdbcPersistence.setAdapter(new MySqlJDBCAdapter());

		// Create a basic datasource (org.apache.commons.dbcp.BasicDataSource) and 
		// set the JDBC properties on it.

		BasicDataSource bds = new BasicDataSource();

		bds.setUrl("jdbc:mysql://localhost/activemq?relaxAutoCommit=true");

		bds.setDriverClassName("com.mysql.jdbc.Driver");

		bds.setUsername("root");

		bds.setPassword("");
		
		bds.setPoolPreparedStatements(true);

		// Set the datasource on the JDBC persistence adapter.
		jdbcPersistence.setDataSource(bds);
		
		m_broker.setPersistenceAdapter(jdbcPersistence);		
	}
	
	
	private void startDeliveryToQueue() {
		QueueProducer producer = null;
        try {
        	producer = new QueueProducer("tcp://localhost:61616",
"ARCHIVER_QUEUE");
        } catch (JMSException e1) {
            WrCheck.logWarn(e1);            
        } 
        
        for(;;) {
            try {            	
        		String time = String.valueOf(System.currentTimeMillis());                
                WrCheck.logInfo("Sending queue message --> <" + time + ">");                
                producer.sendMessage(time);
            	
            } catch (JMSException ex) {
                WrCheck.logWarn(ex);
                                
                try {
                    WrCheck.logInfo("WebServerSimulator going to sleep...");
                    Thread.sleep(10 * 1000);
                    
                    producer.restart();
                } 
                catch (Exception e) {
                    WrCheck.logWarn(e);
                }
            }            
            catch (Exception exc) {
                WrCheck.logFatal(exc);                
            }
            
            try {
                Thread.sleep((long) (Math.random() * 3000));
            } catch (InterruptedException e) {
                WrCheck.logWarn(e);               
            }
        }
	}
	
	
	public static void main(String[] args) throws Exception {
		EmbeddedBroker eb = new EmbeddedBroker();
		eb.go();		
	}
}

Best regards.
-- 
View this message in context: http://www.nabble.com/Embedded-Broker---configuration-via-Java-tf3888599s2354.html#a11029347
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Embedded Broker - configuration via Java

Posted by Raffaele <r....@prismasw.it>.


James.Strachan wrote:
> 
> I meant to disable JMX not JMS  :)
> 

Eheheh, with disabling JMS we go all at home...

Ok, thanks, it was that I started the embeddedbroker with jdk 1.4, and so
the solution was to set useJMX to false, thanks again.

Best regards

Raffaele

-- 
View this message in context: http://www.nabble.com/Embedded-Broker---configuration-via-Java-tf3888599s2354.html#a11023623
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Embedded Broker - configuration via Java

Posted by James Strachan <ja...@gmail.com>.
On 6/8/07, James Strachan <ja...@gmail.com> wrote:
> On 6/8/07, Raffaele <r....@prismasw.it> wrote:
> >
> > Hi all,
> >
> > I'm trying to create, configure and launch a broker all in one class.
> >
> > I have a ClassNotFoundException executing the following lines:
> >
> > BrokerService broker = new BrokerService();
> > broker.addConnector("tcp://localhost:61616");
> > broker.start();
> >
> > And the exception thrown by addConnector(...) is this:
> >
> > java.lang.NoClassDefFoundError:
> > javax/management/MalformedObjectNameException
> >         at
> > org.apache.activemq.broker.BrokerService.getManagementContext(BrokerService.java:696)
> >         at
> > org.apache.activemq.broker.BrokerService.createRegionBroker(BrokerService.java:1265)
> >         at
> > org.apache.activemq.broker.BrokerService.createBroker(BrokerService.java:1209)
> >         at
> > org.apache.activemq.broker.BrokerService.getBroker(BrokerService.java:508)
> >         at
> > org.apache.activemq.broker.BrokerService.addConnector(BrokerService.java:163)
> >         at
> > org.apache.activemq.broker.BrokerService.addConnector(BrokerService.java:153)
> >         at pss.jms.EmbeddedBroker.main(EmbeddedBroker.java:49)
> >
> > In my simple test project I'm including only the apache-activemq-4.*.jar
> > found in the activemq home directory.
> > 1) Which other jar should I include?
>
> Looks like you also need JMX - or Java 5 (which comes with JMX). You
> could just disable JMS via broker.setUseJmx(false).

I meant to disable JMX not JMS  :)

-- 
James
-------
http://macstrac.blogspot.com/

Re: Embedded Broker - configuration via Java

Posted by James Strachan <ja...@gmail.com>.
On 6/8/07, Raffaele <r....@prismasw.it> wrote:
>
> Hi all,
>
> I'm trying to create, configure and launch a broker all in one class.
>
> I have a ClassNotFoundException executing the following lines:
>
> BrokerService broker = new BrokerService();
> broker.addConnector("tcp://localhost:61616");
> broker.start();
>
> And the exception thrown by addConnector(...) is this:
>
> java.lang.NoClassDefFoundError:
> javax/management/MalformedObjectNameException
>         at
> org.apache.activemq.broker.BrokerService.getManagementContext(BrokerService.java:696)
>         at
> org.apache.activemq.broker.BrokerService.createRegionBroker(BrokerService.java:1265)
>         at
> org.apache.activemq.broker.BrokerService.createBroker(BrokerService.java:1209)
>         at
> org.apache.activemq.broker.BrokerService.getBroker(BrokerService.java:508)
>         at
> org.apache.activemq.broker.BrokerService.addConnector(BrokerService.java:163)
>         at
> org.apache.activemq.broker.BrokerService.addConnector(BrokerService.java:153)
>         at pss.jms.EmbeddedBroker.main(EmbeddedBroker.java:49)
>
> In my simple test project I'm including only the apache-activemq-4.*.jar
> found in the activemq home directory.
> 1) Which other jar should I include?

Looks like you also need JMX - or Java 5 (which comes with JMX). You
could just disable JMS via broker.setUseJmx(false).


> 2) Is there a page on your confluence where it is explained how to configure
> a broker ALL using only Java without any xml and without Spring?

http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html

further configuration options can be seen on the BrokerService javadoc
http://activemq.apache.org/maven/activemq-core/apidocs/org/apache/activemq/broker/BrokerService.html

-- 
James
-------
http://macstrac.blogspot.com/