很久不更新这里了2009-05-26 21:58:50

很久不更新这里了,这是我自己写的第一个版本的博客程序,是能生成静态页面的,现在也没有勇气去将代码再重构一下。这几年到处写博客,写来写去还是不安定。

把spring配置打包到jar包里提供给不同消费者使用 2007-08-01 09:19:09

 第一步

将spring配置打包写好
书写beanRefContext.xml
demo
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="no" default-lazy-init="false"
    default-dependency-check="none">

    <bean id="businessBeanFactory"
        class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
            <list>
                <value>applicationContext-datasource.xml</value>
                <value>applicationContext-hibernate.xml</value>
                <value>applicationContext-service.xml</value>
                <value>applicationContext-xstream.xml</value>
            </list>
        </constructor-arg>
    </bean>
   
    <bean id="ear.context"
        class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
            <list>
                <value>applicationContext-quartz.xml</value>
                <value>applicationContext-ibmmq-client.xml</value>
                <value>applicationContext-jms-client.xml</value>
                <value>applicationContext-xstream.xml</value>
                <value>applicationContext-mail.xml</value>
            </list>
        </constructor-arg>
    </bean>
   
</beans>

此jar包命名为service.jar包,service.jar包位于ear包中并确保ejb的ejb.manifest.classpath中存在
businessBeanFactory 提供给ejb
在spring对ejb支持的类中

    public void setMessageDrivenContext(
            MessageDrivenContext messageDrivenContext) {
        super.setMessageDrivenContext(messageDrivenContext);
        String locatorFactorySelector = null;
        String beanFactoryLocatorKey = null;
        try {
            locatorFactorySelector = (String) new JndiTemplate()
                    .lookup("java:comp/env/locatorFactorySelector");
        } catch (NamingException e) {
            e.printStackTrace();
        }

        setBeanFactoryLocator(locatorFactorySelector != null ? ContextSingletonBeanFactoryLocator
                .getInstance(locatorFactorySelector)
                : ContextSingletonBeanFactoryLocator.getInstance());// 指定默认的beanRefContext.xml

        try {
            beanFactoryLocatorKey = (String) new JndiTemplate()
                    .lookup("java:comp/env/beanFactoryLocatorKey");
        } catch (NamingException e) {
            e.printStackTrace();
        }
        setBeanFactoryLocatorKey(beanFactoryLocatorKey != null ? beanFactoryLocatorKey
                : "businessBeanFactory");
    }


ejb配置

<description>ejb</description>
   <display-name>ejb</display-name>
      <enterprise-beans>
       <message-driven id="MessageDriven_1">
         <description>yourEJB</description>
         <display-name>Name for
yourEJB</display-name>
         <ejb-name>yourEJB</ejb-name>
         <ejb-class>com.corp.YourEJB</ejb-class>
         <transaction-type>Bean</transaction-type>        
         <message-driven-destination>
            <destination-type>javax.jms.Queue</destination-type>
         </message-driven-destination>
     <env-entry>
          <env-entry-name>locatorFactorySelector</env-entry-name>
          <env-entry-type>java.lang.String</env-entry-type>
          <env-entry-value>classpath*:beanRefContext.xml</env-entry-value>
      </env-entry>
      <env-entry>
          <env-entry-name>beanFactoryLocatorKey</env-entry-name>
          <env-entry-type>java.lang.String</env-entry-type>
          <env-entry-value>businessBeanFactory</env-entry-value>
      </env-entry>
      </message-driven>
    </enterprise-beans>
   
    <assembly-descriptor id="AssemblyDescriptor_1">
       </assembly-descriptor>
</ejb-jar>  

//传入参数指定加载的bean工厂的名字

ear.context 提供给web

service.jar必须存在web的lib下或者在
manifest.classpath
ejb中这样加载
<context-param>
    <param-name>locatorFactorySelector</param-name>
    <param-value>classpath*:beanRefContext.xml</param-value>
</context-param>
<context-param>
        <param-name>parentContextKey</param-name>
        <param-value>ear.context</param-value>
</context-param>
<!--但我不知道再加载另外的配置能否跟其融和在一起?
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-blank.xml</param-value>
</context-param>
-->

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

Spring对消息驱动Bean的支持开发EJB2版的消息驱动Bean 2007-06-21 23:21:59

第1步
开发基于POJI的POJO的业务实现,最基本的我们经常那么干的

第2步
使用Spring的xml解耦合这些POJO实现
产生applicationContext-datasource.xml 等

第3步
消息驱动Bean开发继承自org.springframework.ejb.support.AbstractMessageDrivenBean
给消息驱动Bean注入第二步的Beanfactory,在onMessage(Message message)中调用beanFactory中的
POJO实现业务处理,消息驱动Bean是Spring工厂的消费类。

第4步
部署消息驱动Bean,消息驱动Bean是JMS的消息处理Bean

第5步
书写特定容器的配置文件绑定JNDI

附开发实例文件如下

weblogic-ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN" "http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd">
<weblogic-ejb-jar>
 <description>Test</description>
   <weblogic-enterprise-bean>
      <ejb-name>MyMessageDrivenBean</ejb-name>
      <message-driven-descriptor>
          <pool>
            <max-beans-in-free-pool>8</max-beans-in-free-pool>
            <initial-beans-in-free-pool>1</initial-beans-in-free-pool>
          </pool>
       <destination-jndi-name>jms/DestMyMessageQueue</destination-jndi-name>
        <initial-context-factory>weblogic.jndi.WLInitialContextFactory</initial-context-factory>
        <provider-url>t3://localhost:7001</provider-url>
        <!-- 如果JMS和EJB容器不在一台机器上指定该JMS provider-url-->
        <connection-factory-jndi-name>jms/MyMessageDrivenBean</connection-factory-jndi-name>
        <jms-polling-interval-seconds>10</jms-polling-interval-seconds>
        <!--jms-client-id>myMessageDrivenBean</jms-client-id-->
      </message-driven-descriptor>
   </weblogic-enterprise-bean>
</weblogic-ejb-jar>


META-INF/ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar id="ejb-jar_1">
<description>ejb</description>
   <display-name>ejb</display-name>
      <enterprise-beans>
       <message-driven id="MessageDriven_1">
         <description>MyMessageDrivenBean</description>
         <display-name>Name for MyMessageDrivenBean</display-name>
         <ejb-name>MyMessageDrivenBean</ejb-name>
         <ejb-class>service.MyMessageDrivenBean</ejb-class>
         <transaction-type>Bean</transaction-type>
         <message-driven-destination>
            <destination-type>javax.jms.Queue</destination-type>
         </message-driven-destination>
      </message-driven>
    </enterprise-beans>
            <assembly-descriptor id="AssemblyDescriptor_1">
            </assembly-descriptor>
</ejb-jar>           

消息驱动Bean加载所需要的POJObeanFactory的Bean资源
beanRefContext.xml

    <bean id="businessBeanFactory"
        class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
            <list>
                <value>applicationContext-datasource.xml</value>
                <value>applicationContext.xml</value>
                <value>applicationContext-service.xml</value>           
            </list>
        </constructor-arg>
    </bean>

消息驱动Bean
MyMessageDrivenBean.java

package service;

import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
import org.springframework.ejb.support.AbstractMessageDrivenBean;

public class MyMessageDrivenBean extends AbstractMessageDrivenBean implements
        MessageDrivenBean, MessageListener {

    private static final long serialVersionUID = 7819604050800044554L;

    private static Log log = LogFactory.getLog(MyMessageDrivenBean.class); // log

    /**
     * for spring inject this property
     */
    private MessageHandelService messageHandelService;

    public MessageHandelService getMessageHandelService() {
        return messageHandelService;
    }

    public void setMessageHandelService(
            MessageHandelService messageHandelService) {
        this.messageHandelService = messageHandelService;
    }

    protected void onEjbCreate() {
        this.setMessageHandelService((MessageHandelService) getBeanFactory()
                .getBean("messageHandelService"));
        /**
         * 从spring 工厂bean中得到需要的业务实现,业务实现是基于POJI的POJO实现
         * 本身消息驱动Bean只负责暴露接口JNDI来接受JMS的Queue并调用POJI的POJO实现处理业务逻辑
         * 就是真正的业务逻辑是写在POJO中实现的 消息驱动Bean相对于Spring来说是Bean消费者
         */
    }

    public void onMessage(Message message) {

        if (message instanceof ObjectMessage) { // 如果接受的消息是序列化后的对象,直接强制转换
            ObjectMessage objMessage = (ObjectMessage) message;
            MyDomain model = null;
            try {
                model = (MyDomain) objMessage.getObject();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
        // 业务处理调用接口
        this.getMessageHandelService().doFoo();

        if (message instanceof TextMessage) {

        }

    }

    public void setMessageDrivenContext(
            MessageDrivenContext messageDrivenContext) {
        super.setMessageDrivenContext(messageDrivenContext);
        setBeanFactoryLocator(ContextSingletonBeanFactoryLocator.getInstance());// 指定默认的beanRefContext.xml
        setBeanFactoryLocatorKey("businessBeanFactory");
    }

}

weblogic8.13配置JMS的步骤2007-06-20 23:42:29

weblogic8.13配置JMS的步骤

1)进入consle控制台
2)mydomain->服务->JMS->连接工厂->配置新的JMS Connection Factory :指定JNDI名称 jms/SendJMSFactory,客户端ID,指定目标和部署myserver
暂时不指定客户端ID,因为MessageDrivenBean跟消息发送端都用到这个JMSConnectionFactory会抛出weblogic.jms.common.InvalidClientIDException
3)mydomain->服务->JMS->存储->配置新的JMS File Store :指定目录
4)mydomain->服务->服务器->配置新的JMS Server :选择第3步配置好的持久性存储 ,指定目标和部署myserver
5)给配置好的MyJMS Server->目标->配置新的 JMS Queue 指定 JNDI jms/SendJMSQueue


消息发送端
服务器URL: t3://localhost:7001
连接工厂: jms/SendJMSFactory
队列: jms/SendJMSQueue

可以点击mydomain->服务器->myserver->常规->查看JNDI树 查看时否已经有了两个jms的JNDI,有则表示部署成功了




每一个消息驱动EJB监听一个关联的JMS目的地。如果
这个JMS目的地被部署在另外一个Weblogic服务器实例
或者是外部的JMS提供者,这样得不到JMS目的地,这种情况下
EJB容器自动试探去连接JMS服务器,如果JMS重新启动,消息驱动Bean
就又可以接受消息了

jms-polling-interval-seconds 定义扫描监听JMS的间隔时间,默认10秒

6)使用Spring开发JMS消息发送端



JMSSender.java

package.com.corp.service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
 * @author Administrator
 */
public class JMSSender {

    private static Log log = LogFactory.getLog(JMSSender.class);

    private JmsTemplate jmsTemplate;

    /**
     * @return Returns the jmsTemplate.
     */
    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }

    /**
     * @param jmsTemplate
     *            The jmsTemplate to set.
     */
    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public void sendMessage(final String s) {
        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {

                log.debug("Creating the message from loan parameters.");
                TextMessage textMessage = session.createTextMessage(s);

                return textMessage;
            }
        });
    }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-lazy-init="true">

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
            <prop key="java.naming.provider.url">t3://localhost:7001</prop>
        </props>
    </property>
</bean>

<bean id="jmsQueueConnectionFactory"
      class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
    </property>
    <property name="jndiName">
        <value>jms/SendJMSFactory</value>
    </property>
</bean>

<bean id="sendDestination"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
    </property>
    <property name="jndiName">
        <value>jms/SendJMSQueue</value>
    </property>
</bean>


<bean id="jmsTemplate"
      class="org.springframework.jms.core.JmsTemplate102">
    <property name="connectionFactory">
        <ref bean="jmsQueueConnectionFactory"/>
    </property>
    <property name="defaultDestination">
        <ref bean="sendDestination"/>
    </property>
    <property name="receiveTimeout">
        <value>30000</value>
    </property>
</bean>

<bean id="jmsSender" class="com.corp.service.JMSSender">
    <property name="jmsTemplate">
        <ref bean="jmsTemplate"/>
    </property>
</bean>
</beans>



index.jsp
调用JMSSender发送消息


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


<%
org.springframework.beans.factory.BeanFactory beanFactory = (org.springframework.beans.factory.BeanFactory)
pageContext.getServletContext().
getAttribute(org.springframework.web.context.
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);


com.corp.service.JMSSender jmsSender = (com.loanapp.service.JMSSender)beanFactory.getBean("jmsSender");

jmsSender.sendMessage("Helloworld");


%>

</body>
</html>

JAVA 竖线|转义字符2007-03-25 17:57:18

JAVA 竖线|转义字符 |

 

在使用String.split方法分隔字符串时,分隔符如果用到一些特殊字符,可能会得不到我们预期的结果。

public String[] split(String regex)
Splits this string around matches of the given regular expression.
参数regex是一个 regular-expression的匹配模式而不是一个简单的String,他对一些特殊的字符可能会出现你预想不到的结果,比如测试下面的代码:

用竖线 | 分隔字符串,你将得不到预期的结果

    String[] aa = "aaa|bbb|ccc".split("|");
    //String[] aa = "aaa|bbb|ccc".split("|"); 这样才能得到正确的结果

    for (int i = 0 ; i <aa.length ; i++ ) {
      System.out.println("--"+aa);
    }

用竖 * 分隔字符串运行将抛出java.util.regex.PatternSyntaxException异常,用加号 + 也是如此。

    String[] aa = "aaa*bbb*ccc".split("*");
    //String[] aa = "aaa|bbb|ccc".split("*"); 这样才能得到正确的结果   

    for (int i = 0 ; i <aa.length ; i++ ) {
      System.out.println("--"+aa);
    }

显然,+ * 不是有效的模式匹配规则表达式,用"*" "+"转义后即可得到正确的结果。

"|" 分隔串时虽然能够执行,但是却不是预期的目的,"|"转义后即可得到正确的结果。

还有如果想在串中使用""字符,则也需要转义.首先要表达"aaaabbbb"这个串就应该用"aaaabbbb",如果要分隔就应该这样才能得到正确结果:

String[] aa = "aaabbbbccc".split("");

首页 更多主题 只显示标题