public class ProducerApp extends ProducerTool {
protected int messageSize = 1024 * 8;
private long timeToLive = 50000;
Session session;
MessageProducer producer;
Connection connection;
TextMessage message;
public ProducerApp() {
}
protected MessageProducer createProducer(Session session)
throws JMSException {
MessageProducer producer = session.createProducer(destination);
if (durable) {
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
} else {
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
if (timeToLive != 0)
producer.setTimeToLive(timeToLive return producer;
}
public boolean createMessageText(String messageName) {
try {
message = session.createTextMessage(messageName);
String msg = message.getText();
System.out.println("Sending message: " + msg);
return true;
} catch (JMSException e) {
System.out.println("Caught: " + e);
e.printStackTrace();
return false;
}
}
public boolean sendMessage() {
try {
producer.send(message);
if (transacted) {
session.commit();
}
return true;
} catch (JMSException e) {
System.out.println("Caught: " + e);
e.printStackTrace();
return false;
}
}
public boolean producertoolClose() {
try {
System.out.println("Trying to close the producer");
close(connection, session);
System.out.println("The producer has been closed");
return true;
} catch (JMSException e) {
System.out.println("Caught: " + e);
e.printStackTrace();
return false;
}
}
}
public class ProducerTool {
protected Destination destination;
protected String subject = "demo";
protected boolean topic = true;
protected String user = ActiveMQConnection.DEFAULT_USER;
protected String pwd = ActiveMQConnection.DEFAULT_PASSWORD;
protected String url = "">
protected boolean transacted = false;
protected String clientID;
protected boolean durable = false;
protected int ackMode = Session.AUTO_ACKNOWLEDGE;
protected String consumerName = "James";
public ProducerTool(){
}
protected Session createSession(Connection connection) throws Exception {
Session session = connection.createSession
(transacted, ackMode);
if (topic) {
destination = session.createTopic(subject);
} else {
destination = session.createQueue(subject);
}
return session;
}
protected Connection createConnection() throws JMSException, Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, pwd, url);
Connection connection = connectionFactory.createConnection();
if (durable && clientID != null) {
connection.setClientID(clientID);
}
connection.start();
return connection;
}
protected void close(Connection connection, Session session)
throws JMSException {
// lets dump the stats
dumpStats(connection);
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}
protected void dumpStats(Connection connection) {
ActiveMQConnection c = (ActiveMQConnection) connection;
c.getConnectionStats().dump(new IndentPrinter());
}
}
All above are codes of the bundle intend to communicate with the ActiveMQ Server.Before start this bundle,I have start the ActiveMQ Server.
The result is :
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE system.bundle_3.2.2.R32x_v20070118
1 ACTIVE org.eclipse.osgi.services_3.1.100.v20060601
3 ACTIVE org.eclipse.equinox.servlet.api_1.0.0.v20060601
33 ACTIVE
org.eclipse.equinox.http_1.0.2.R32x_v20061218
38 ACTIVE OSGIProducer_1.0.0
39 RESOLVED ProducerApp_1.0.0
40 ACTIVE ProducerImplOne_1.0.0
osgi> start 39
org.osgi.framework.BundleException: Exception in producerapp.Activator.start() of bundle ProducerApp.
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java
:1010)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:966)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:317)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start
(AbstractBundle.java:256)
at org.eclipse.osgi.framework.internal.core.FrameworkCommandProvider._start(FrameworkCommandProvider.java:239)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.osgi.framework.internal.core.FrameworkCommandInterpreter.execute
(FrameworkCommandInterpreter.java:145)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.docommand(FrameworkConsole.java:293)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.console(FrameworkConsole.java
:278)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(FrameworkConsole.java:213)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: javax/jms/JMSException
at
producerapp.Activator.start(Activator.java:43)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$2.run(BundleContextImpl.java:991)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator
(BundleContextImpl.java:985)
... 13 more
Nested Exception:
java.lang.NoClassDefFoundError: javax/jms/JMSException
at producerapp.Activator.start(Activator.java:43)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$2.run
(BundleContextImpl.java:991)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:985)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start
(BundleContextImpl.java:966)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:317)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:256)
at
org.eclipse.osgi.framework.internal.core.FrameworkCommandProvider._start(FrameworkCommandProvider.java:239)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.osgi.framework.internal.core.FrameworkCommandInterpreter.execute
(FrameworkCommandInterpreter.java:145)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.docommand(FrameworkConsole.java:293)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.console(FrameworkConsole.java
:278)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(FrameworkConsole.java:213)
at java.lang.Thread.run(Unknown Source)
Nested Exception:
java.lang.NoClassDefFoundError: javax/jms/JMSException
at producerapp.Activator.start(Activator.java:43)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$2.run(BundleContextImpl.java:991)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:985)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:966)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker
(BundleHost.java:317)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:256)
at org.eclipse.osgi.framework.internal.core.FrameworkCommandProvider._start(FrameworkCommandProvider.java
:239)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke
(Unknown Source)
at org.eclipse.osgi.framework.internal.core.FrameworkCommandInterpreter.execute(FrameworkCommandInterpreter.java:145)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.docommand(FrameworkConsole.java
:293)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.console(FrameworkConsole.java:278)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(FrameworkConsole.java:213)
at java.lang.Thread.run
(Unknown Source)
Regards
--
Transit Wang/Wang Zhu
Computer School,Northwestern Polytechnical University