Am trying to run a simple JMS app, sadly got NullPointException . following are the links to the files of the app :
1. Producer.java
2. SynchConsumer.java
Tools used : Eclipse & GlassFish
following is the exception
Exception in thread "main" java.lang.NullPointerException
at coreservlets.Producer.main(Producer.java:96)
Any flash put into this shall be appreciated .
If you run your stand-alone application without any container around it, the #Resource injections will not work. Instead you have to do manual JNDI lookups for both the connection factory and the queue/topic:
Example:
final Properties initialContextProperties = new Properties();
final String factory = "jms/ConnectionFactory";
final String queueName = "jms/Queue";
//
final InitialContext ic = new InitialContext(initialContextProperties);
final QueueConnectionFactory qcf = (QueueConnectionFactory) ic
.lookup(factory);
final Queue queue = (Queue) ic.lookup(queueName);
As for the configuration of the InitialContext, look here: Glassfish V3.x and remote standalone client
connectionFactory.createConnection();
connectionFactory, even if static, is never initialized. For this reason it is null and you get the null pointer when you try to access the method inside it.
Two way of fixing.
If the method is static, don't declare the var, just call:
ConnectionFactory.createConnection();
If the method is not static (strange, for a class that is static), you have to do something like:
private ConnctionFactory connectionFactory = new ConnectionFactory();
Related
1) Difference between the constructors of InitialContext.
public InitialContext(Hashtable<?,?> environment)
what does this constructor do and what will environment parameter do .
2)
Hashtable<Object, String> environment= new Hashtable<Object, String> (2);
--
--
Context ctx = new InitialContext( environment);
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setJdbcUrl(----);
comboPooledDataSource.setDriverClass(----);
ctx.bind (__);
please explain each line what does it do..
3)Why to create combopooldatasource object ,instead we can create datasource object..??
Answer for 3rd question : Datasource can not be created for ordinary domains,its only for JNDI domains.
For 1st : InitialContext is used to set the environment to create the pooled datasource by creating .bindings file
For 2nd : Context sets the environment.
ComboPooledDataSource object is created.
For that object the url and jdbc driver class wll be set to take the connections from DB to the Pool.
Above set things will be binded to the context and it will be wriiten in .bindings file in encoded format
I want to create Queue and the MessageDrivenBean in Oracle 11g.
I created the JMS Module on Weblogic and into it I created Queue and ConnectionFactory.
JDBC names looks like:
Queue: jms/EvZahQueue
ConnectionFactory: jms/ConnectionFactory
I tried to get them with:
Context context = new InitialContext();
connectionFactory = (QueueConnectionFactory) context.lookup("jms/QueueConnector");
queue = (Queue) context.lookup("jms/EvZahQueue");
But, I've got an exception like this:
javax.naming.NameNotFoundException: While trying to look up comp/env/jms/QueueConnector in /app/webapp/registri-view/31900933.; remaining name 'comp/env/jms/QueueConnector'
Also, I tried with:
Context context = new InitialContext();
connectionFactory = (QueueConnectionFactory) context.lookup("java:comp/env/jms/QueueConnector");
queue = (Queue) context.lookup("java:comp/env/jms/EvZahQueue");
And I tried to create default properties and to put them into new InitialContext() but nothing changed.
What should I do? Maybe I need to write something in web.xml, ejb-jar.xml, weblogic-ejb-jar.xml?
From the WebLogic console, Environment -> Servers -> View
JNDI Tree.
From there, find the queue, take note of the nested path where it's
located, then use that path in the lookup.
I resolve the problem.
In Weblogic, I must create subdeployment of my JMS module (Oracle said it is not mandatory, but in my case it seems it is) and then everything works fine.
"javax.naming.Context" is commonly used inside Java EE development. It's quite convenient to use it to establish dynamical database connection by calling its lookup function with given names of resources inside context.xml. The sample code is shown as following where "db_name" is the name you used to identify the database resource.
Context ctx = new InitialContext();
DataSource ds = ctx.lookup("java:comp/env/jdbc/db_name");
My concern is what are the differences between lookup resources by using the same context and lookup resources by using different contexts. And which approach makes more sense or suitable? Suppose all database resources are defined inside the same context.xml file. For example:
Context ctx = new InitialContext();
DataSource ds1 = ctx.lookup("java:comp/env/jdbc/db_name_ds1");
DataSource ds2 = ctx.lookup("java:comp/env/jdbc/db_name_ds2");
and
Context ctx_ds1 = new InitialContext();
Context ctx_ds2 = new InitialContext();
DataSource ds1 = ctx_ds1.lookup("java:comp/env/jdbc/db_name_ds1");
DataSource ds2 = ctx_ds2.lookup("java:comp/env/jdbc/db_name_ds2");
Thank you for your sharing.
there is no difference except you created an unnecessary extra java object. however, if the jndi server was remote, you would have created two different network connections and iverhead of managing them - definetly not what one should do.
I am trying to use a datasource that I set up on my weblogic server.
datasource JNDI name = thinOracleDataSource
in my code I have the following
public class DAOBean implements java.io.Serializable {
private Connection conn;
public void connect() throws ClassNotFoundException,
SQLException, NamingException {
Context ctx = new InitialContext();
// Lookup using JNDI name.
DataSource ds = (javax.sql.DataSource) ctx.lookup("thinOracleDataSource");
conn = ds.getConnection();
}
But I get this error
javax.naming.NameNotFoundException: While trying to look up /thinOracleDataSource in /app/webapp/PreAssignment2/24911485.; remaining name '/thinOracleDataSource'
am I looking the JNDI name in the right way? or am I missing something? Thanks for any help!!
EDIT:
This is the jndi tree that I can get from the weblogic console
Try naming your datasource jdbc/thisOracleDataSource in Weblogic and reference it as:
DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/thinOracleDataSource");
Also, make sure the datasource is "targeted" to your weblogic Java server. All of this can be done in the Weblogic admin console.
Your JNDI key should look like approximately "java:comp/env/jdbc/thinOracleDataSource".
You can verify it by using Weblogic console that allows access (and probably search) in JNDI. So, you can check this manually before writing the code.
I am using a GlassFish-3.1.2 server running in my subnet (192.168.1.3:3700). I already deployed an enterprise app including an EJB in which i defined a business method. Now I want to remotely access the EJB from my java application client. How do i have to setup the JNDI resp. the InitialContext object for doing the lookup of the EJB ? How do I need to define the properties? Btw. I had to run "asadmin enabled-secure-admin" in order to make the GlassFish server work on the LAN. Probably I also need to send my credentials with the properties ?
Here's my current "solution", which seems to be completley wrong :
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.1.3");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext(props);
TestentityFacadeRemote tfr = (TestentityFacadeRemote)ctx.lookup("java:global/TestEE/TestEE-ejb/TestentityFacadeRemote");
When I run this programm, it just waits infinitely...
Any help highly appreciated!
I solved the problem by setting the host and port directy by System.setProperty() and using the default constructor for initializing the InitialContext(). Note that the following lines should be the very first in your program / main method:
public static void main(String[] args) {
System.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.1.3");
System.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext();
TestentityFacadeRemote tfr = (TestentityFacadeRemote)ctx.lookup("java:global/TestEE/TestEE-ejb/TestentityFacadeRemote!com.acme.remote.TestentityFacade");
}
Hope this helps ...