当前位置: 首页 > 知识库问答 >
问题:

Mdb Glasswish Websphere MQ

寇夜洛
2023-03-14

我是MDB和EE的新手。请告诉我我错了。我的应用程序必须与Websphere MQ交互(等待队列中的消息,执行某些操作并回复)。我使用的是NetBeans 7.3、GlassFish 3.1、Websphere MQ 6.2、Resorte适配器wmq.jmsra.rar。交互必须不是jms格式的,只有Web MQ的性质。我正在部署适配器,创建连接池和管理对象。在domain.xml中

  <connector-connection-pool description="" name="cpMqAdapter" resource-adapter-name="wmq.jmsra" connection-definition-name="javax.jms.QueueConnectionFactory" transaction-support="LocalTransaction">
  <property name="port" value="1414"></property>
  <property name="CCSID" value="866"></property>
  <property name="hostName" value="192.168.0.11"></property>
  <property name="queueManager" value="QM"></property>
  <property name="channel" value="SrvConn"></property>
  <property description="CLIENT - mq on other computer" name="transportType" value="CLIENT"></property>
</connector-connection-pool>


<admin-object-resource enabled="false" res-adapter="wmq.jmsra" res-type="javax.jms.Queue" description="" jndi-name="wmqJmsAOR" class-name="com.ibm.mq.connector.outbound.MQQueueProxy">
  <property name="priority" value="APP"></property>
  <property name="failIfQuiesce" value="true"></property>
  <property name="baseQueueManagerName" value="QM"></property>
  <property name="CCSID" value="1208"></property>
  <property name="persistence" value="APP"></property>
  <property name="encoding" value="NATIVE"></property>
  <property name="baseQueueName" value="TEST"></property>
  <property name="targetClient" value="MQ"></property>
  <property name="expiry" value="APP"></property>
</admin-object-resource>

在netbeans中,我正在创建EE项目和消息驱动Bean。我得到了这个代码

   @MessageDriven(mappedName = "wmqJmsAOR", activationConfig = {
   @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto- 
   acknowledge"),
   @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")

 })
public class NewMessage implements MessageListener {
public NewMessage() {
    super();
    System.out.println("I created");
}

@Override
public void onMessage(Message message) {

    System.out.println("I'm getting message");

}
`

请告诉我为什么这个MDB不是listenig队列(我将测试消息放在Websphere MQ控制台中)。可能是我必须在config中写一些东西(现在作为默认netbeans创建的项目)。

阿列克谢

共有1个答案

甄德寿
2023-03-14

我有一个有效的解决方案。这不是最好的解决方案,但它确实非常有效。

我们所做的是创建一个非常简单的ActivationSpecWrapper类来扩展IBM com.IBM.mq.connector.Inbound.ActivationSpecImpl类。此包装类有一个公共Set/Get属性(asJNDI)。如果类的目的是通过JNDI上下文读取App server中定义的Properties类,它包含要在激活MDB时分配的所有属性。

首先,创建新的ActivationSpecWrapper类。你可以把这个放进你选择的任何一个包里。

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import com.ibm.mq.connector.inbound.ActivationSpecImpl;

public class ActivationSpecWrapper extends ActivationSpecImpl
{

   private static final long   serialVersionUID = -529716553593856979L;

   private static final String sourceClass      = ActivationSpecWrapper.class.getName();
   private static final Logger log              = Logger.getLogger(sourceClass);

   private String              asJNDI           = null;

   public void setAsJNDI(String asJNDI)
   {
      log.config("asJNDI = " + asJNDI);
      this.asJNDI = asJNDI;

      try
      {
         final InitialContext ctx = new InitialContext();
         final Properties properties = (Properties) ctx.lookup(asJNDI);

         for (final Object key : properties.keySet())
         {
            try
            {
               final String value = properties.getProperty((String) key);
               final Object field = getSetter((String) key);
               if (field != null)
               {
                  if (field instanceof Field)
                  {
                     log.fine("Setting " + key + " via Field " + (String) key + " = " + value);
                     ((Field) field).set(this, value);

                  }
                  else
                  {
                     log.fine("Setting " + key + " via Method " + (String) key + " = " + value);
                     ((Method) field).invoke(this, value);
                  }
                  log.config(key + " = " + value);
               }
               else
               {
                  log.warning("Invalid ActivationSpec Field: " + key);
               }
            }
            catch (final NoSuchFieldException e)
            {
               log.throwing(sourceClass, "setAsJNDI", e);
            }

         }

      }
      catch (final Exception e)
      {
         log.log(Level.SEVERE, "Error looking up " + asJNDI, e);
         return;
      }

   }

   public String getAsJNDI()
   {
      return asJNDI;
   }

   private static Object getField(String fieldName) throws NoSuchFieldException
   {
      return ActivationSpecWrapper.class.getField(fieldName);
   }

   private static Object getSetter(String fieldName) throws NoSuchFieldException
   {
      try
      {
         final StringBuilder sb = new StringBuilder(fieldName.length() + 3).append("set").append(fieldName);
         sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));

         return ActivationSpecWrapper.class.getMethod(sb.toString(), String.class);
      }
      catch (final NoSuchMethodException e)
      {
         return getField(fieldName);
      }
   }
}
: jar -xvf wmq.jmsra.rar META-INF/ra.xml
: perl -pi -e 's/com\.ibm\.mq\.connector\.inbound\.ActivationSpecImpl/your.new.package.ActivatonSpecWrapper/g' META-INF/ra.xml
: jar -uvf wmq.jmsra.rar META-INF/ra.xml
<activationspec>
   <activationspec-class>
      com.ibm.mq.connector.inbound.ActivationSpecImpl
   </activationspec-class>
   <required-config-property>
      <config-property-name>destination</config-property-name>
   </required-config-property>
   <required-config-property>
      <config-property-name>destinationType</config-property-name>
   </required-config-property>
</activationspec>

更改之后,meta-inf/ra.xml应该类似于:

<activationspec>
   <activationspec-class>
      your.new.package.ActivatonSpecWrapper
   </activationspec-class>
   <required-config-property>
      <config-property-name>destination</config-property-name>
   </required-config-property>
   <required-config-property>
      <config-property-name>destinationType</config-property-name>
   </required-config-property>
</activationspec>

现在需要将新包添加到RAR文件中。它应该是标准目录结构。像这样:

: jar -uvf wmq.jmsra.rar your/new/package/ActivationSpecWrapper.class

问题源于IBM将主机/端口/队列管理器/通道(等等)而不是administration对象。它属于管理对象,因为它是MDB队列的连接工厂。IBM在那里只允许两个属性。

<?xml version="1.0" encoding="UTF-8"?>
<glassfish-ejb-jar>

    <enterprise-beans >
        <unique-id>1</unique-id>
        <ejb>
            <ejb-name>MyMDB</ejb-name>
            <mdb-resource-adapter>
                <resource-adapter-mid>wmq.jmsra</resource-adapter-mid>
                <activation-config>
                    <activation-config-property>
                        <activation-config-property-name>asJNDI</activation-config-property-name>
                        <activation-config-property-value>mq/InboundMessages</activation-config-property-value>
                    </activation-config-property>
                </activation-config>
            </mdb-resource-adapter>
        </ejb>
    </enterprise-beans> 
</glassfish-ejb-jar>
@MessageDriven(activationConfig =
   {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/InboundMessage_queue"),
    @ActivationConfigProperty(propertyName = "useJNDI", propertyValue = "true") })
public class MyMDB implement MessageListener
{
    public void onMessage(Message message)
    {
        // message handler code goes here...
    }
}
<custom-resource res-type="java.util.Properties" jndi-name="mq/InboundMessages" factory-class="org.glassfish.resources.custom.factory.PropertiesFactory">
  <property name="hostName" value="mqserver"></property>
  <property name="port" value="1422"></property>
  <property name="queueManager" value="MQMNGR"></property>
  <property name="channel" value="MQMNGR.SM.S1"></property>
  <property name="transportType" value="CLIENT"></property>
</custom-resource>

希望这能帮上忙。这不是最简单的解决方案,但它足够简单,并且一旦建立了它,它就非常可移植,并且允许应用服务器管理员管理到MQ的连接细节,而不是开发人员。

 类似资料:

相关问答

相关文章

相关阅读