Reputation: 2135
I am really struggling to get a working bare minimum code for a JMS producer, I have got my WL JMS server up and running and ready to test it using a JMS client but for creating a client I am having issues with use of initial context factory i.e. env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
. I have tried almost all code, using WL initial context factory, oracle j2ee initial context factory (env.put(Context.INITIAL_CONTEXT_FACTORY, "oracle.j2ee.rmi.RMIInitialContextFactory");
) but I always get no class definition found exception when I run my code. For example, for below code I get no class definition found exception.
I am trying to run client as a standalone Java program. I understand the exception and have tried to add the relevant JAR for example when I was using WL initial context factory then I placed WL full client JAR in the classpath but still I am not able to overcome this no class def found exception.
Could someone please link to some repository or blog or provide me a bare minimum "working" JMS producer, or point on what wrong I am doing.
Please note that my JMS server is WL, and I am trying to create a simple JMS client, without using ActiveMQ library, but I am tagging the ActiveMQ as well so that it can get more attention but if anyone thinks it is wrong then please let me know and I will delete ActiveMQ tag, or please feel free to delete yourself.
My client example:
import java.util.Hashtable;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JmsProducerQueueClient {
public static void main(String[] args) throws NamingException, JMSException {
Connection connection = null;
try {
System.out.println("Create JNDI Context");
Context context = getInitialContext();
System.out.println("Get connection facory");
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("/com/jms/ms1/cf1");
System.out.println("Create connection");
connection = connectionFactory.createConnection();
System.out.println("Create session");
Session session = connection.createSession(false, QueueSession.AUTO_ACKNOWLEDGE);
System.out.println("Lookup queue");
Queue queue = (Queue) context.lookup("/com/jms/ms1/q1");
System.out.println("Start connection");
connection.start();
System.out.println("Create producer");
MessageProducer producer = session.createProducer(queue);
System.out.println("Create hello world message");
Message hellowWorldText = session.createTextMessage("Hello World!");
System.out.println("Send hello world message");
producer.send(hellowWorldText);
} finally {
if (connection != null) {
System.out.println("close the connection");
connection.close();
}
}
}
public static Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
//env.put(Context.INITIAL_CONTEXT_FACTORY, "oracle.j2ee.rmi.RMIInitialContextFactory");
env.put(Context.PROVIDER_URL, "tcp://localhost:6007");
Context context = new InitialContext(env);
return context;
}
}
Logs:
Create JNDI Context
Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/security/service/PrivilegedActions
at weblogic.jndi.WLSJNDIEnvironmentImpl.<clinit>(WLSJNDIEnvironmentImpl.java:57)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at weblogic.jndi.internal.JNDIEnvironment.getJNDIEnvironment(JNDIEnvironment.java:37)
at weblogic.jndi.Environment.<clinit>(Environment.java:92)
at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:117)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at com.learning.so.question.jms.queue.producerconsumer.JmsProducerQueueClient.getInitialContext(JmsProducerQueueClient.java:62)
at com.learning.so.question.jms.queue.producerconsumer.JmsProducerQueueClient.main(JmsProducerQueueClient.java:22)
Upvotes: 0
Views: 1432
Reputation: 1377
I managed to make your code work on my side, so I think that your problem comes from a missing dependency, pretty sure from the weblgogic client. May be the way you packaged your application is the problem.
So I will expose what I did, just using my own queue name et factory name.
Note that this test has been done on WLS 12.1.3.
Firstly I created a maven dependency from the wlthint3client.jar
that can be found on weblogic server, in ${ORACLE_HOME}/wlserver/server/lib
using the mvn install:install-file
command :
mvn install:install-file -Dfile=wlthint3client.jar -DgroupId=weblogic -DartifactId=wlthint3client -Dversion=12.1.3.0.0 -Dpackaging=jar
With that, I can now use the following dependency in all my maven projects :
<dependency>
<groupId>weblogic</groupId>
<artifactId>wlthint3client</artifactId>
<version>12.1.3.0.0</version>
</dependency>
Then I created a JMSXAFactory
Factory and a test
JMS Queue on weblogic side.
Then I created the maven project. Here is the pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jvi.weblogic.jms</groupId>
<artifactId>jms-producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jms-producer</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>weblogic</groupId>
<artifactId>wlthint3client</artifactId>
<version>12.1.3.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And the App
class code :
package org.jvi.weblogic.jms;
import java.util.Hashtable;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* Hello world!
*
*/
public class App
{
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public final static String URL = "t3://localhost:7001";
public static void main( String[] args ) throws NamingException, JMSException
{
System.out.println( "Hello World!" );
Connection connection = null;
try {
System.out.println("Create JNDI Context");
Context context = getInitialContext();
System.out.println("Get connection facory");
ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("JMSXAFactory");
System.out.println("Create connection");
connection = connectionFactory.createConnection();
System.out.println("Create session");
Session session = connection.createSession(false, QueueSession.AUTO_ACKNOWLEDGE);
System.out.println("Lookup queue");
Queue queue = (Queue) context.lookup("test");
System.out.println("Start connection");
connection.start();
System.out.println("Create producer");
MessageProducer producer = session.createProducer(queue);
System.out.println("Create hello world message");
Message hellowWorldText = session.createTextMessage("Hello World!");
System.out.println("Send hello world message");
producer.send(hellowWorldText);
} finally {
if (connection != null) {
System.out.println("close the connection");
connection.close();
}
}
}
private static InitialContext getInitialContext() throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, URL);
return new InitialContext(env);
}
}
Then when I launch the application through command line :
java -cp jms-producer-0.0.1-SNAPSHOT.jar org.jvi.weblogic.jms.App
After launching this command, I can see that message is correctly produced in test
queue :
Upvotes: 1
Reputation: 4323
Your code looks ok, but you are currently missing a class in your classpath: java.lang.NoClassDefFoundError: weblogic/security/service/PrivilegedActions
check here, this might have the answer: Weblogic 12.1.3 PrivilegedActions class not found
Upvotes: 1