2012-11-30 280 views
2

我只是測試,以發送郵件的GlassFish服務器隊列,以便爲我有這樣的創造JMSResource GlassFish服務器上:發送消息

enter image description here

,現在我已經創建了一個NetBeans IDE中的Java應用程序。這是我的Java文件: -

import javax.annotation.Resource; 
    import javax.jms.Connection; 
import javax.jms.ConnectionFactory; 
import javax.jms.JMSException; 
import javax.jms.MessageProducer; 
import javax.jms.Queue; 
import javax.jms.Session; 
import javax.jms.TextMessage; 

public class MessageSender 
{ 
    @Resource(mappedName = "jms/ConnectionFactory") 
    private static ConnectionFactory connectionFactory; 
    @Resource(mappedName = "jms/Queue") 
    private static Queue queue; 

    public void produceMessages() 
    { 
    MessageProducer messageProducer; 
    TextMessage textMessage; 
    try 
    { 
     Connection connection = connectionFactory.createConnection(); 
     Session session = connection.createSession(false, 
     Session.AUTO_ACKNOWLEDGE); 
     messageProducer = session.createProducer(queue); 
     textMessage = session.createTextMessage(); 

     textMessage.setText("Testing, 1, 2, 3. Can you hear me?"); 
     System.out.println("Sending the following message: " 
     + textMessage.getText()); 
     messageProducer.send(textMessage); 

     messageProducer.close(); 
     session.close(); 
     connection.close(); 
    } 
    catch (JMSException e) 
    { 
     e.printStackTrace(); 
    } 
    } 
    public static void main(String[] args) 
    { 
    new MessageSender().produceMessages(); 
    } 
} 

現在,當我運行這個簡單的應用程序,然後它給上創建的ConnectionFactory錯誤:

Exception in thread "main" java.lang.NullPointerException 
    at simplesendamq.MessageSender.produceMessages(MessageSender.java:33) 
    at simplesendamq.MessageSender.main(MessageSender.java:55) 
+0

線33在上面的代碼中是應該是一個線空行(33行發生NullPointerException)。請張貼您的應用程序中的確切代碼。另一件事 - 你把你的課放在什麼樣的項目中?企業應用程序客戶端還是其他? –

回答

0

GF客戶端必須使用JNDI,查找獲取連接工廠 和消息目的地(主題/隊列)。因此,您必須形成JNDI上下文(您設置主機,ORB端口,其他魔術參數),並在類路徑中包含某些庫(它需要與GF一起工作)。

我的例子:

Receiver.java

import javax.jms.*; 
import javax.naming.Context; 
import javax.naming.InitialContext; 
import java.util.Properties; 

    public class Receiver implements MessageListener { 
     public static void main(String[] args) throws JMSException, NamingException { 
      try { 
       Properties props = new Properties(); 

       props.setProperty("java.naming.factory.initial", 
            "com.sun.enterprise.naming.SerialInitContextFactory"); 
       props.setProperty("java.naming.factory.url.pkgs", 
            "com.sun.enterprise.naming"); 
       props.setProperty("java.naming.factory.state", 
            "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl"); 
       props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost"); 
       props.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); 

       // Gets the JNDI context 
       Context jndiContext = new InitialContext(props); 

       // Looks up the administered objects 
       ConnectionFactory connectionFactory = (ConnectionFactory) 
         jndiContext.lookup("my.connection.factory"); 

       Queue queue = (Queue) jndiContext.lookup("my.test.queue"); 

       // Creates the needed artifacts to connect to the queue 
       Connection connection = connectionFactory.createConnection(); 

       Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); 

       MessageConsumer consumer = session.createConsumer(queue); 

       connection.start(); 

      } catch(Exception e) { 
       e.printStackTrace(); 
      } 
     } 

    @Override 
    public void onMessage(Message message) { 
     System.out.println("onMsg!"); 
    } 
    } 

的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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>examples.my</groupId> 
    <artifactId>jms-test</artifactId> 
    <version>1.0</version> 

    <dependencies> 
     <dependency> 
      <groupId>com.sun.messaging</groupId> 
      <artifactId>imqjmsra</artifactId> 
      <version>4.4</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.jms</groupId> 
      <artifactId>jms-api</artifactId> 
      <version>1.1-rev-1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.common</groupId> 
      <artifactId>common-util</artifactId> 
      <version>3.0</version> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.appclient</groupId> 
      <artifactId>gf-client</artifactId> 
      <version>3.0-b74b</version> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.appclient.server</groupId> 
      <artifactId>appclient-server-core</artifactId> 
      <version>3.0-b74b</version> 
     </dependency> 

     <dependency> 
      <groupId>com.sun.corba</groupId> 
      <artifactId>glassfish-corba-orb</artifactId> 
      <version>3.0.1-b001</version> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.security</groupId> 
      <artifactId>security</artifactId> 
      <version>3.0-b74b</version> 
     </dependency> 
    </dependencies> 

</project>