顯示具有 Java EE 標籤的文章。 顯示所有文章
顯示具有 Java EE 標籤的文章。 顯示所有文章

2011年8月23日 星期二

使用Maven遇到"java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file"錯誤

筆者在使用JAX-RS (Jersey)及Java EE 6 API時,執行"mvn test",會出現類似以下的錯誤
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ws/rs/core/MediaType
...
原因是Java EE 6只是一個Interface,必須要等到hibernate jars讀取進來,實作這些JPA interface,因此要調整Java-ee-api在pom.xml裡面的位置,才不會出現這個錯誤。


	com.sun.jersey
	jersey-server
	1.0.1




	org.hibernate
	hibernate-core
	3.6.6.Final



	org.hibernate
	hibernate-entitymanager
	3.6.6.Final



	org.hibernate
	hibernate-validator
	4.0.2.GA




	javax
	javaee-api
	6.0
	provided

Reference: stackoverflow

2011年6月6日 星期一

JMS: Resource Injection

Within the Java EE environment, objects bound within the Java EE context can use the @Resource annotation to inject the JMS resources into the code rather than performing a JNDI lookup using the InitialContext.
From Java Message Service

public class Lender {

  TopicConnection conn = null;
  TopicSession session = null;
  Topic ratesTopic = null;
  TopicPublisher publisher = null;

  public Lender() {
    try {
      
      Context ctx = new InitialContext();

      TopicConnectionFactory factory =
        (TopicConnectionFactory)ctx.lookup("TopicCF");
      conn = factory.createTopicConnection();
      conn.start();
      session =
        conn.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
      ratesTopic = (Topic)ctx.lookup("jms/Rates");
      publisher = session.createPublisher(ratesTopic);
      ...
    } catch (JMSException jmse) {
      jmse.printStackTrace();
    } catch (NamingException jne) {
      jne.printStackTrace();
    }
  }
  ...
}

若使用Java EE所提供的Annotation,注入JMS Resource,則不需要在程式中使用InitialContext作JNDI的lookup。

@Stateless
@Remote(Lender.class)
public class LenderBean implements Lender {
  
  @Resource(name="TopicCF") TopicConnectionFactory factory;
  @Resource(name="jms/Rates") Topic ratesTopic;

  TopicConnection conn = null;
  TopicSession session = null;
  TopicPublisher publisher = null;

  @PostContruct
  public void init() {
    try {
      conn = factory.createTopicConnection();
      conn.start();
      session =
        conn.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
      publisher = session.createPublisher(ratesTopic);
      ...
    } catch (JMSException jmse) {
      jmse.printStackTrace();
    }
  } 
  ...
}

Reference: Java Message Service