paulwong

#

软件知识大学

如果你对UML感兴趣,建议你按以下顺序学习:
1.UML入门课程:
http://www.umlonline.org/school/forum-25-1.html
2.在线课程:活用UML-需求分析高手:(需金币)
http://www.umlonline.org/school/forum-26-1.html
3.UML视频课程
http://www.umlonline.org/school/forumdisplay.php?fid=128&filter=type&typeid=12
4.在线课程:活用UML-软件设计高手(需金币)
http://www.umlonline.org/school/forum-27-1.html

如果你对设计模式感兴趣:
1.设计模式学院:
http://www.umlonline.org/school/index.php?gid=5

如果你对需求分析感兴趣,建议你学习:
1.视频课程:需求分析挑战之旅
http://www.umlonline.org/school/thread-361-1-1.html
2.在线课程:需求分析挑战之旅-疯狂的订餐系统
http://www.umlonline.org/school/forum-111-1.html
3.在线课程:活用UML-需求分析高手(需金币)
http://www.umlonline.org/school/forum-26-1.html
4.视频课程:活用类图,拥抱需求
http://www.umlonline.org/school/thread-466-1-1.html
5.在线课程:需求能力提升训练营(需金币)
http://www.umlonline.org/school/forum-159-1.html

如果你对软件设计感兴趣,建议你学习:
1.视频课程:做一回软件架构师
http://www.umlonline.org/school/thread-589-1-1.html
2.设计模式学院:
http://www.umlonline.org/school/index.php?gid=5
3.在线课程:活用UML-软件设计高手(需金币)
http://www.umlonline.org/school/forum-27-1.html


如果你对软件项目管理(敏捷开发)感兴趣,建议你学习:
1.在线课程:项目管理入门
http://www.umlonline.org/school/forum-115-1.html
2.一系列项目管理相关视频课程
http://www.umlonline.org/school/forumdisplay.php?fid=128&filter=type&typeid=10
3.视频课程:项目管理极速提升(需金币)
http://www.umlonline.org/school/thread-669-1-1.html
4.在线课程:超越敏捷——超级项目管理
http://www.umlonline.org/school/forum-117-1.html

如果你对CMMI(过程改进)感兴趣,建议你学习:
1.视频课程:畅游CMMI
http://www.umlonline.org/school/thread-468-1-1.html
2.扫盲文章:CMMI基础知识扫盲
http://www.umlonline.org/school/thread-73-1-1.html
3.在线课程:Intro to CMMI 中国版
http://www.umlonline.org/school/forum-91-1.html
4.在线课程:CMMI快乐之旅(内容未完整)
http://www.umlonline.org/school/forum-92-1.html

posted @ 2011-12-05 11:23 paulwong 阅读(289) | 评论 (0)编辑 收藏

UML中几种类间关系:继承、实现、依赖、关联、聚合、组合的联系与区别

这是一堂关于UML基础知识的补习课;现在我们做项目时间都太紧了,基本上都没有做过真正的class级别的详细设计,更别提使用UML来实现规范建模了;本篇主要就以前自己一直感觉很迷糊的几种class之间的关系进行整理,让我们在真正用UML进行比如类图设计时能够更加清晰明了;以下就分别介绍这几种关系:

继承

指的是一个类(称为子类、子接口)继承另外的一个类(称为父类、父接口)的功能,并可以增加它自己的新功能的能力,继承是类与类或者接口与接口之间最常见的关系;在Java中此类关系通过关键字extends明确标识,在设计时一般没有争议性;

实现

指的是一个class类实现interface接口(可以是多个)的功能;实现是类与接口之间最常见的关系;在Java中此类关系通过关键字implements明确标识,在设计时一般没有争议性;

依赖

可以简单的理解,就是一个类A使用到了另一个类B,而这种使用关系是具有偶然性的、、临时性的、非常弱的,但是B类的变化会影响到A;比如某人要过河,需要借用一条船,此时人与船之间的关系就是依赖;表现在代码层面,为类B作为参数被类A在某个method方法中使用;

关联

他体现的是两个类、或者类与接口之间语义级别的一种强依赖关系,比如我和我的朋友;这种关系比依赖更强、不存在依赖关系的偶然性、关系也不是临时性的,一般是长期性的,而且双方的关系一般是平等的、关联可以是单向、双向的;表现在代码层面,为被关联类B以类属性的形式出现在关联类A中,也可能是关联类A引用了一个类型为被关联类B的全局变量;

聚合

聚合是关联关系的一种特例,他体现的是整体与部分、拥有的关系,即has-a的关系,此时整体与部分之间是可分离的,他们可以具有各自的生命周期,部分可以属于多个整体对象,也可以为多个整体对象共享;比如计算机与CPU、公司与员工的关系等;表现在代码层面,和关联关系是一致的,只能从语义级别来区分;

组合

组合也是关联关系的一种特例,他体现的是一种contains-a的关系,这种关系比聚合更强,也称为强聚合;他同样体现整体与部分间的关系,但此时整体与部分是不可分的,整体的生命周期结束也就意味着部分的生命周期结束;比如你和你的大脑;表现在代码层面,和关联关系是一致的,只能从语义级别来区分;

对于继承、实现这两种关系没多少疑问,他们体现的是一种类与类、或者类与接口间的纵向关系;其他的四者关系则体现的是类与类、或者类与接口间的引用、横向关系,是比较难区分的,有很多事物间的关系要想准备定位是很难的,前面也提到,这几种关系都是语义级别的,所以从代码层面并不能完全区分各种关系;但总的来说,后几种关系所表现的强弱程度依次为:组合>聚合>关联>依赖;

posted @ 2011-12-02 02:13 paulwong 阅读(401) | 评论 (0)编辑 收藏

JDK1.5中的线程池(java.util.concurrent.ThreadPoolExecutor)使用简介

大多数并发应用程序是以执行任务(task)为基本单位进行管理的。通常情况下,我们会为每个任务单独创建一个线程来执行。这样会带来两个问题:一,大量的线程(>100)会消耗系统资源,使线程调度的开销变大,引起性能下降;二,对于生命周期短暂的任务,频繁地创建和消亡线程并不是明智的选择。因为创建和消亡线程的开销可能会大于使用多线程带来的性能好处。

一个比较简单的线程池至少应包含线程池管理器、工作线程、任务队列、任务接口等部分。其中线程池管理器(ThreadPool Manager)的作用是创建、销毁并管理线程池,将工作线程放入线程池中;工作线程是一个可以循环执行任务的线程,在没有任务时进行等待;任务队列的作用是提供一种缓冲机制,将没有处理的任务放在任务队列中;任务接口是每个任务必须实现的接口,主要用来规定任务的入口、任务执行完后的收尾工作、任务的执行状态等,工作线程通过该接口调度任务的执行。下面的代码实现了创建一个线程池,以及从线程池中取出线程的操作。

在多线程大师Doug Lea的贡献下,在JDK1.5中加入了许多对并发特性的支持,例如:线程池。

1.核心线程(任务):我们定义的线程,即实现了Runnable接口的类,是我们将要放到线程池中执行的类,如实例代码中的CountService类

2.工作线程:由线程池中创建的线程,是用来获得核心线程并执行核心线程的线程(比较拗口哦,具体看代码就知道是什么东东了)。

简单理解就三个概念:线程、线程池和任务。

任务:就是要执行的业务逻辑;
线程:任务是要放到线程中去执行的;
线程池:主要是控制当前正在执行的线程的数量和将要被执行的线程队列。

一、简介
线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为:

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,long keepAliveTime, TimeUnit unit,BlockingQueue<Runnable> workQueue,RejectedExecutionHandler handler)


corePoolSize: 线程池维护线程的最少数量
maximumPoolSize:线程池维护线程的最大数量
keepAliveTime: 线程池维护线程所允许的空闲时间
unit: 线程池维护线程所允许的空闲时间的单位
workQueue: 线程池所使用的缓冲队列
handler: 线程池对拒绝任务的处理策略

一个任务通过 execute(Runnable)方法被添加到线程池,任务就是一个 Runnable类型的对象,任务的执行方法就是 Runnable类型对象的run()方法。

当一个任务通过execute(Runnable)方法欲添加到线程池时:

如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。

如果此时线程池中的数量等于 corePoolSize,但是缓冲队列 workQueue未满,那么任务被放入缓冲队列。

如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maximumPoolSize,建新的线程来处理被添加的任务。

如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maximumPoolSize,那么通过 handler所指定的策略来处理此任务。

也就是:处理任务的优先级为:
核心线程corePoolSize、任务队列workQueue、最大线程maximumPoolSize,如果三者都满了,使用handler处理被拒绝的任务。

当线程池中的线程数量大于 corePoolSize时,如果某线程空闲时间超过keepAliveTime,线程将被终止。这样,线程池可以动态的调整池中的线程数。

unit可选的参数为java.util.concurrent.TimeUnit中的几个静态属性:
NANOSECONDS、MICROSECONDS、MILLISECONDS、SECONDS。

workQueue我常用的是:java.util.concurrent.ArrayBlockingQueue

handler有四个选择:

ThreadPoolExecutor.AbortPolicy()
抛出java.util.concurrent.RejectedExecutionException异常

ThreadPoolExecutor.CallerRunsPolicy()
重试添加当前的任务,他会自动重复调用execute()方法

ThreadPoolExecutor.DiscardOldestPolicy()
抛弃旧的任务

ThreadPoolExecutor.DiscardPolicy()
抛弃当前的任务

二、一般用法举例


package com.paul.threadPool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThreadPool {

private static int produceTaskSleepTime = 10;

private static int produceTaskMaxNumber = 10;

public static void main(String[] args) {

// 构造一个线程池
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(3),
new ThreadPoolExecutor.CallerRunsPolicy());

for (int i = 1; i <= produceTaskMaxNumber; i++) {
try {
String task
= "task@ " + i;
System.out.println(
"创建任务并提交到线程池中:" + task);
threadPool.execute(
new ThreadPoolTask(task));

Thread.sleep(produceTaskSleepTime);
}
catch (Exception e) {
e.printStackTrace();
}

}

}

}


package com.paul.threadPool;

import java.io.Serializable;

public class ThreadPoolTask implements Runnable, Serializable {

private static final long serialVersionUID = 0;

// 保存任务所需要的数据
private Object threadPoolTaskData;

private static int consumeTaskSleepTime = 2000;

ThreadPoolTask(Object tasks)
{
this.threadPoolTaskData = tasks;
}


public synchronized void run() {
// 处理一个任务,这里的处理方式太简单了,仅仅是一个打印语句
System.out.println("开始执行任务:" + threadPoolTaskData);
try {
// //便于观察,等待一段时间
Thread.sleep(consumeTaskSleepTime);
}
catch (Exception e) {
e.printStackTrace();
}

threadPoolTaskData
= null;
}


public Object getTask() {
return this.threadPoolTaskData;
}


}


说明:
1、在这段程序中,一个任务就是一个Runnable类型的对象,也就是一个ThreadPoolTask类型的对象。
2、一般来说任务除了处理方式外,还需要处理的数据,处理的数据通过构造方法传给任务。
3、在这段程序中,main()方法相当于一个残忍的领导,他派发出许多任务,丢给一个叫 threadPool的任劳任怨的小组来做。
这个小组里面队员至少有两个,如果他们两个忙不过来,任务就被放到任务列表里面。
如果积压的任务过多,多到任务列表都装不下(超过3个)的时候,就雇佣新的队员来帮忙。但是基于成本的考虑,不能雇佣太多的队员,至多只能雇佣 4个。
如果四个队员都在忙时,再有新的任务,这个小组就处理不了了,任务就会被通过一种策略来处理,我们的处理方式是不停的派发,直到接受这个任务为止(更残忍!呵呵)。
因为队员工作是需要成本的,如果工作很闲,闲到 3SECONDS都没有新的任务了,那么有的队员就会被解雇了,但是,为了小组的正常运转,即使工作再闲,小组的队员也不能少于两个。
4、通过调整 produceTaskSleepTime和 consumeTaskSleepTime的大小来实现对派发任务和处理任务的速度的控制,改变这两个值就可以观察不同速率下程序的工作情况。
5、通过调整4中所指的数据,再加上调整任务丢弃策略,换上其他三种策略,就可以看出不同策略下的不同处理方式。
6、对于其他的使用方法,参看jdk的帮助,很容易理解和使用。

posted @ 2011-12-02 01:07 paulwong 阅读(472) | 评论 (0)编辑 收藏

一个使用GUZZ+SPRING的MAVEN项目POM文件

     摘要: 有依赖的包管理,可以部署到TOMCAT 6.X,可以使用JETTY作开发测试,可以生成站点,作CHECK STYLE,PMD代码检查,代码覆盖率,生成JAVA DOC。 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><project ...  阅读全文

posted @ 2011-11-30 16:18 paulwong 阅读(1499) | 评论 (2)编辑 收藏

在Eclipse中调试Maven项目

使用maven的一个方便之处是可以使用Jetty Plugin来运行web项目。只要maven jetty:run就可以把web项目跑起来了。只是很多时候我们都需要在IDE中进行调试。那如何在Eclipse中调试使用jetty Plugin的web项目呢?
下面我们就来配置一下。
  1. 首先在Run->Externel Tools->Open Externel Tools Dialog.. 打开配置对话框,选中左边的Program节点,右键选择New然后再右边的配置里面输入Name信息,在Main tab下的Location选择你maven可执行文件的全路径(eg:/home/rory/apps/apache-maven-2.0.8/bin/mvn),Working Directory选择你的maven项目(eg:${workspace_loc:/guice-example}),Arguments里输入jetty:run。然后再切换到Environment tab。New 一下变量,name是
     
    MAVEN_OPTS

    value是
    -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8080,server=y,suspend=y

    ok,这一步设置完毕,Apply一下,close关闭

  2. 接下来添加一个Debug,打开Run->Open Debug Dialog..打开Debug Dialog窗口,选中左边的Remote Java Application,右键New输入name,Project里选中要Debug的项目Connection Properties里的Host里设置成localhost,Port设置成上面配置的8080(这里要填JETTY默认启动的端口)然后Apply一下就ok了。

  3. 接下来就可以开始调试了。首先启动第一步配置的Externel Tools配置,然后再运行第二步配置的Debug.就可以看到控制台有mvn jetty:run的输出了。接下来就开如Debug你的项目吧。:)

posted @ 2011-11-30 16:02 paulwong 阅读(4598) | 评论 (2)编辑 收藏

实践:搭建基于maven的Java EE协同开发环境

http://dearshor.iteye.com/blog/272274

posted @ 2011-11-30 00:15 paulwong 阅读(394) | 评论 (0)编辑 收藏

基于全注解方式的SSH基础框架

http://www.chinasb.org/archives/2011/07/2961.shtml 

posted @ 2011-11-26 04:32 paulwong 阅读(335) | 评论 (0)编辑 收藏

Hibernate Generic D.A.O. Framework

The motivation behind the framework

We had worked on a project where we hand-coded all of our DAOs. This produced four irksome difficulties: (1) Method names and implementations were not altogether consistent. (2) It was a pain to make additional columns sortable or filterable, and as a result, a lot of pages lacked good sorting and filtering. (3) Making additional DAOs was tedious and took a fair amount of time. (4) Writing tests for DAOs is tricky and tedious, so no one really did.

This framework aims to ease our troubles.

Why might you consider looking into this framework?

  • Generic DAO: With the sweetness of Java generics, the concept of generic DAOs is not new, and it’s not difficult. However, we’ve put a lot of work into making these easy to use and robust. So if you like the way we’ve done it, then this framework provides ready-made code for you. On the other hand if you’d rather make your own, then simply feel free to look at our source code for ideas and help on a few potentially tricky issues.
  • Search: Search is the most original and sophisticated part of this framework, and it can be used with or without the generic DAO portion of the framework. The search capability is designed around the use-case of a list page with sorting, filtering, column selection and paging. However, its use is certainly not limited to that. The value that the search adds is simpler, more robust querying with less coding and less testing. It is similar to Hibernate Criteria, but it is simpler to use and can easily move across layers of an application including view and even remote layers. Plus is works with both Hibernate and JPA*.
  • Remote DAO (for R.I.A.s?): If you you’re like us, you don’t want to write and configure an individual DAO style remote service for each entity in a R.I.A. or other client-heavy application. This framework may have the solution. We provide utilities and sample code to adapt our single general DAO to any remoting technology interface. Just configure this single remote access point and the client can do any basic CRUD or search operation on any entity. Again, if you don’t like our way of doing things, maybe you can at least get some ideas from our source code.
  • Remote Search (for R.I.A.s?): As mentioned above, the framework can provide a single point for client-server CRUD and search operations. The framework’s search is meant to be able to cross the client-server boundary. So lists and searches in the client application can take advantage of the same easy-to-use features and consistency that the search functionality provides in the server tier or single tier application. This consistency allowed us to create a reusable collection type in Adobe Flex 3 that is associated with a single search object and automatically updates itself from the server according to the search parameters.**

*A fairly simple adapter is required for each JPA provider. Right now we only have an adapter for Hibernate Entity Manager. If anyone would like to contribute an adapter for any other JPA provider (OpenJPA, TopLink, etc.), that would be great.

**If time permits, we would like to eventually post our corresponding Adobe Flex 3 framework and utilities.

More Information

Wiki Documentation: UserGuide

Javadoc: http://hibernate-generic-dao.googlecode.com/svn/trunk/docs/api/index.html

Blog: http://hibernategenericdao.wordpress.com/

Questions and Comments

Please post at http://groups.google.com/group/java-generic-dao.

Code Examples

Creating DAOs for individual model classes:

Simply extend the GenericDAO class with the specific type.


public interface ProjectDAO extends GenericDAO<Project, Long> 

}
 

public class ProjectDAOImpl extends GenericDAOImpl<Project, Long> implements ProjectDAO 

}

The following methods (and several more) are now available on ProjectDAO

Project project = projectDAO.find(projectId);
 List
<Project> list = projectDAO.findAll();
projectDAO.save(project);
projectDAO.remove(project);
projectDAO.removeById(project.getId());

Search search
= new Search();
search.addFilterEqual(
"name", "hibernate-generic-dao");
List
<Project> list = projectDAO.search(search);

int count = projectDAO.count(search);
SearchResult
<Project> result = projectDAO.searchAndCount(search);
list
= result.getResult();
count
= result.getTotalCount();
search.clear();
search.addField(
"rating", Field.OP_AVG);
int avgProjectRating = (Integer) prjoectDAO.searchUnique(search);


A GeneralDAO is also provided with DAO methods for any entity:


public interface GeneralDAO {
public <T> T find(Class<T> type, Serializable id);
public <T> T[] find(Class<T> type, Serializable ids);
public <T> T getReference(Class<T> type, Serializable id);
public <T> T[] getReferences(Class<T> type, Serializable ids);
public boolean save(Object entity); public boolean[] save(Object entities);
public boolean remove(Object entity); public void remove(Object entities);
public boolean removeById(Class<?> type, Serializable id);
public void removeByIds(Class<?> type, Serializable ids);
public <T> List<T> findAll(Class<T> type);
public List search(ISearch search); public Object searchUnique(ISearch search);
public int count(ISearch search); public SearchResult searchAndCount(ISearch search);
public boolean isAttached(Object entity);
public void refresh(Object entities); public void flush();
public Filter getFilterFromExample(Object example);
public Filter getFilterFromExample(Object example, ExampleOptions options); }

Search DTO usage examples


Search search = new Search(Project.class);

//filtering
search.addFilterEqual("name", "hibernate-generic-dao");

search.addFilterLessThan(
"completionDate", new Date());

search.addFilterOr(
Filter.equal(
"name", "Jack"),
Filter.and(
Filter.equal(
"name", "Jill"),
Filter.like(
"location", "%Chicago%"),
Filter.greaterThan(
"age", 5) )
);

search.addFilterIn(
"name", "Jack", "Jill", "Bob");

search.addFilterNot(Filter.in(
"name","Jack", "Jill", "Bob"));

//sorting
search.addSort("name");
search.addSort(
"age", true); //descending

//projection
search.addField("name");
search.addField(
"location");

//or with column operators
search.addField("rating", Field.OP_AVG);
search.addField(
"developerCount", Field.OP_MAX);

//paging
search.setMaxResults(15); //a.k.a. results per page
search.setPage(3);

//controlling eager fetching of relationships
serach.addFetch("owner");
 

Nested properties are also fully supported...


search.addFilterEqua("status.name", "active");
search.addFilterGreaterThan(
"workgroup.manager.salary", 75000.00);
search.addSort(
"status.name");

posted @ 2011-11-26 04:23 paulwong 阅读(1189) | 评论 (0)编辑 收藏

用MAVEN搭建JAVAEE5项目架构

Installation

Step 1 (download):

Download the file javaee5-maven-archetype-1.0.jar

Step 2 (install in local repo):

Run:

mvn install:install-file -Dfile=javaee5-maven-archetype-1.0.jar -DgroupId=pl.piotrsorobka.maven -DartifactId=javaee5-maven-archetype -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true

Using the archetype

Run:

mvn archetype:create -DarchetypeGroupId=pl.piotrsorobka.maven -DarchetypeArtifactId=javaee5-maven-archetype -DarchetypeVersion=1.0 -DgroupId=your.group.id -DartifactId=project-name

Deploy on Glassfish

Step 1:

Set environment variable GLASSFISH_HOME, pointing to your Glassfish installation folder. Alternatively, point <glassfishDirectory></glassfishDirectory> in your main pom.xml to your Glassfish installation folder.

Step 2:

Inside your project main folder run:

mvn clean install

and after:

mvn -f ear/pom.xml glassfish:deploy

If your Glassfish settings are default it will deploy the ear.

posted @ 2011-11-25 00:34 paulwong 阅读(1348) | 评论 (0)编辑 收藏

Spring3 and EJB Integration(I)EJB Client

     摘要: Spring3 and EJB Integration(I)EJB Client I used EJB client to call EJB stateless bean, I found it is waste time to lookup the stateless bean every time. So I tried to use spring to get singleton stub ...  阅读全文

posted @ 2011-11-24 21:54 paulwong 阅读(542) | 评论 (0)编辑 收藏

仅列出标题
共116页: First 上一页 94 95 96 97 98 99 100 101 102 下一页 Last