mule源码学习

mule的object-to-jmsmessage全过程。
当end-point接受到来自从component处理后的消息后,就调用配置的<jms:object-to-jmsmessage-transformer name="jmsTransformer" />
比如接受到的是一个list对象,暂且称为owner对象。调用objectToJmsmessage类的transform(MuleMessage message, String outputEncoding)方法
具体如下:
 public Object transform(MuleMessage message, String outputEncoding) throws TransformerException
    {
        try
        {
            if (logger.isDebugEnabled())
            {
                logger.debug("Source object is " + ClassUtils.getSimpleName(message.getPayload().getClass()));
            }

            Object result = transformToMessage(message);

            if (logger.isDebugEnabled())
            {
                logger.debug("Resulting object is " + ClassUtils.getSimpleName(result.getClass()));
            }

            return result;
        }
        catch (Exception e)
        {
            throw new TransformerException(this, e);
        }
    }

    当执行到 Object result = transformToMessage(message)时进入
    protected Message transformToMessage(MuleMessage message) throws TransformerException
    {
        Session session = null;
        try
        {
            Message result;

            Object src = message.getPayload();
            if (src instanceof Message)
            {
                result = (Message) src;
                result.clearProperties();
            }
            else
            {
                session = this.getSession();
                result = JmsMessageUtils.toMessage(src, session);
            }
            this.setJmsProperties(message, result);

            return result;
        }
       
    }

进入session = this.getSession();连接到jms服务器。此时需要保证你的activeMQ或其他的jms服务器启动了。
protected Session getSession() throws TransformerException, ConnectException, JMSException
    {
        if (endpoint != null)
        {
            return ((JmsConnector) endpoint.getConnector()).getSession(endpoint);
        }

进入result = JmsMessageUtils.toMessage(src, session);开始将你传进来的东西转成jmsmessage:
 public static Message toMessage(Object object, Session session) throws JMSException
    {
        if (object instanceof Message)
        {
            return (Message) object;
        }
        else if (object instanceof String)
        {
            return session.createTextMessage((String) object);
        }
        else if (object instanceof Map)
        {
            MapMessage mMsg = session.createMapMessage();
            Map src = (Map) object;

            for (Iterator i = src.entrySet().iterator(); i.hasNext();)
            {
                Map.Entry entry = (Map.Entry) i.next();
                mMsg.setObject(entry.getKey().toString(), entry.getValue());
            }

            return mMsg;
        }
        else if (object instanceof InputStream)
        {
            StreamMessage sMsg = session.createStreamMessage();
            InputStream temp = (InputStream) object;

            byte[] buffer = new byte[4096];
            int len;

            try
            {
                while ((len = temp.read(buffer)) != -1)
                {
                    sMsg.writeBytes(buffer, 0, len);
                }
            }
            catch (IOException e)
            {
                throw new JMSException("Failed to read input stream to create a stream message: " + e);
            }

            return sMsg;
        }
        else if (object instanceof List)
        {
            StreamMessage sMsg = session.createStreamMessage();
            List list = (List) object;
            for (Iterator iter = list.iterator(); iter.hasNext();)
            {
                Object o = iter.next();
                validateStreamMessageType(o);
                sMsg.writeObject(o);
            }
            return sMsg;
        }
        else if (object instanceof byte[])
        {
            BytesMessage bMsg = session.createBytesMessage();
            bMsg.writeBytes((byte[]) object);
            return bMsg;
        }
        else if (object instanceof Serializable)
        {
            ObjectMessage oMsg = session.createObjectMessage();
            oMsg.setObject((Serializable) object);
            return oMsg;
        }
        else if (object instanceof OutputHandler)
        {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            try
            {
                ((OutputHandler) object).write(null, output);
            }
            catch (IOException e)
            {
                JMSException j = new JMSException("Could not serialize OutputHandler.");
                j.initCause(e);
                throw j;
            }
            
            BytesMessage bMsg = session.createBytesMessage();
            bMsg.writeBytes((byte[]) output.toByteArray());
            return bMsg;
        }
        else
        {
            throw new JMSException(
                    "Source was not of a supported type, data must be Serializable, String, byte[], Map or InputStream, " +
                            "but was " + ClassUtils.getShortClassName(object, "<null>"));
        }
    }

由于我传进来的是collector类型的,所以进入了else if (object instanceof List)
        {
            StreamMessage sMsg = session.createStreamMessage();
            List list = (List) object;
            for (Iterator iter = list.iterator(); iter.hasNext();)
            {
                Object o = iter.next();
                validateStreamMessageType(o);
                sMsg.writeObject(o);
            }
            return sMsg;
        }
再进去 validateStreamMessageType(o);看看:
	 protected static void validateStreamMessageType(Object candidate) throws MessageFormatException
    {
        if (candidate instanceof Boolean ||
            candidate instanceof Byte ||
            candidate instanceof Short ||
            candidate instanceof Character ||
            candidate instanceof Integer ||
            candidate instanceof Long ||
            candidate instanceof Float ||
            candidate instanceof Double ||
            candidate instanceof String ||
            candidate instanceof byte[])
        {
            return;
        }

        throw new MessageFormatException(String.format("Invalid type passed to StreamMessage: %s . Allowed types are: " +
                                                       "Boolean, Byte, Short, Character, Integer, Long, Float, Double," +
                                                       "String and byte[]",
                                                       ClassUtils.getShortClassName(candidate, "null")));
    },最后报错,所以以list传入的必须是Boolean, Byte, Short, Character, Integer, Long, Float, Double String and byte[]几种类型的list。

很让人郁闷闷。

你可能感兴趣的:(jms,activemq,J#)