Entity
對應 DB 的 object
POJO plain ordinary java object
不具備任何用途 用於不同地方 就是該角色
DAO data access object
訪問數據庫方法的 interface
2014年1月20日 星期一
2013年9月19日 星期四
marshalling/unmarshalling
傳遞變數的過程。
在不同機器間通過網路傳遞變數(包括Java基本類型和物件),如果目的機器表示資料的方式和原機器不同(ex: 二進位庫),就必須轉譯變數(ex: 序列化),marshalling/unmarshalling 就是傳遞變數的過程。
* Reference
- Java RMI-IIOP - ulinaboy 的部落格 - udn部落格
在不同機器間通過網路傳遞變數(包括Java基本類型和物件),如果目的機器表示資料的方式和原機器不同(ex: 二進位庫),就必須轉譯變數(ex: 序列化),marshalling/unmarshalling 就是傳遞變數的過程。
* Reference
- Java RMI-IIOP - ulinaboy 的部落格 - udn部落格
2013年9月2日 星期一
[Java] Not a Number (NaN)
NaN - Not a Number 表示非定義的數字
ex:
0.0 / 0 (被 0 除的結果)
0.0 / 0.0
不論是和數字或是和 NaN 都不能用關係運算子做比較(<, <=, >, and >=)
Double.isNaN()/Float.isNaN()
* Reference
ex:
0.0 / 0 (被 0 除的結果)
0.0 / 0.0
不論是和數字或是和 NaN 都不能用關係運算子做比較(<, <=, >, and >=)
Double.isNaN()/Float.isNaN()
* Reference
2013年1月6日 星期日
2012年10月10日 星期三
[Java] final in Inner Class
final in Local Inner Class
建立在 Method 中的 inner class。
Local inner class 所在的 Method 回傳值(return)後,Method 中的變數將會被銷毀,因此在 Method 中,被 Local inner class 所用到的變數會被複製一份來使用,此時,如果該變數不是 final,則複製的變數就可能因為更改而不同步,造成錯亂,因此 Local inner class 所用到的 local variables 必須標記為 final。
final in Anonymous Inner Class
匿名式 Anonymous inner class 所用到的 local variables 也必須須標記為 final,因為 Anonymous Inner Class 在實體化時,會將使用到的 local variables 直接複製並且生成為自己的 private 變數,同樣的,出現拷貝時,就會有同步的問題,因此該變數也必須被標記為 final。
* Reference
- InnerClass 的解析 @ 來喝杯JAVA咖啡 :: 痞客邦 PIXNET ::
建立在 Method 中的 inner class。
Local inner class 所在的 Method 回傳值(return)後,Method 中的變數將會被銷毀,因此在 Method 中,被 Local inner class 所用到的變數會被複製一份來使用,此時,如果該變數不是 final,則複製的變數就可能因為更改而不同步,造成錯亂,因此 Local inner class 所用到的 local variables 必須標記為 final。
final in Anonymous Inner Class
匿名式 Anonymous inner class 所用到的 local variables 也必須須標記為 final,因為 Anonymous Inner Class 在實體化時,會將使用到的 local variables 直接複製並且生成為自己的 private 變數,同樣的,出現拷貝時,就會有同步的問題,因此該變數也必須被標記為 final。
* Reference
- InnerClass 的解析 @ 來喝杯JAVA咖啡 :: 痞客邦 PIXNET ::
2012年9月15日 星期六
[Android] Memory Cache
Reference
Strong Reference
WeakReference
weak reference 則是隨時有可能已被回收。
Reference queues
Soft references
Phantom References
Android itself employs some caching as part of the resources infrastructure, so you are already getting the benefit of memory caching.
使用 bitmap 時,記得使用 recycle(),可以幫助釋放空間,所以若是再呼叫 getPixels() or setPixels(),可能會得到 exception,所以建議當不會再使用到 bitmap 時再使用。
LruCache
Note:
Bitmap#getByteCount()
為避免發生 OutOfMemoryError,在系統呼叫 onLowMemory() 時建議呼叫 LruCache#evictAll() 移除所有元素再重建。
Note:
也可以利用 LruCache 來管理在 SD card 中的檔案大小:
* Reference
- Understanding Weak References | Java.net
- Styling Android » Blog Archive » Memory Cache – Part 1
- Styling Android » Blog Archive » Memory Cache – Part 2
- Styling Android » Blog Archive » Memory Cache – Part 3
- Styling Android » Blog Archive » Memory Cache – Part 4
- WeakHashMap的神话 - - ITeye技术网站
- WeakHashMap是如何清除不用的key的
- reference 的可以分為多種強度,我們最熟悉和最常使用的是 strong reference。
- Four different degrees of reference strength: strong, soft, weak, and phantom.
- 強度的差異在於 reference 和 garbage collector 間的互相影響關係。
Strong Reference
// buffer 就是 strong reference StringBuffer buffer = new StringBuffer();
- 當 reference 指向一個以上的物件,便是 strong reference。
- 可以防止此 reference 被 GC。
- 缺點:
- Memory: 因為使得物件不能被回收,所以有造成 memory leak 的危險。
- Caching: 假設在 application 中應用到許多大圖片,為了一直 reload 而 cache 圖片,所以 always 會有 reference 指向圖片,使得它在 memory 且不會被回收,也就是說我們得決定它是否該被移除且使其可以回收。
WeakReference
weak reference 則是隨時有可能已被回收。
// API 使用方式如下,建立 Widget 的 WeakReference。 WeakReferenceWeakHashMapweakWidget = new WeakReference<widget>(widget); // 取出 Widget object。 // 注意: 因為物件可以被回收,所以可能會得到 null。 weakWidget.get()
- 作用類似 HashMap,只是 key 是使用 weak reference,若 key 變成 garbage 則它的 entry 便自動會被移除。
- 但並不是你啥也也沒做他就能自動釋放,而是你使用到 value 才會被釋放。
- super(key, queue); 只有 key 才是 weak reference,value 仍是 strong reference,所以 System.gc() 後,key 會被清除,value 則是才被視為 weak reference 然後到被調用到了才會被清除。
Reference queues
- 當 WeakReference 回傳 null 時表示指向的物件已被回收,WeakReference object 也無用了,所以如 WeakHashMap 會去移除這類的 WeakReference object,避免 WeakHashMap 一直成長,其中卻有很多無用的 WeakReference 。
- ReferenceQueue 可以幫助你管理這些 dead reference。
- 如果你將 ReferenceQueue 傳入 weak reference's constructor,當有 reference 指向的物件被回收時,該 reference 會自動被 insert 到 ReferenceQueue 中,所以你可以定期清裡 ReferenceQueue 也就會清理了 dead reference。
Soft references
- 類似於 weak reference,差異是比 weak reference 不易被回收。
- 因為 soft reference 會盡量被保留在 memory 所以適合做 image cache。
Phantom References
- Its get() method always returns null.
- 用處:
- 可以讓你決定何時將物件從 memory 移除。ex: manipulating large images,你可以先確定現在這一張已被移除,再載入下一張以避免 OOM。
- 避免 finalize() method 可以 "resurrect" 建立了 strong reference 的 object。
- override finalize() 的物件至少要被視為 GC 對象兩次以上才能被回收到,而不能及時的被回收,也因此會有多個待回收的 garbage 存在著等著被回收。
Android itself employs some caching as part of the resources infrastructure, so you are already getting the benefit of memory caching.
使用 bitmap 時,記得使用 recycle(),可以幫助釋放空間,所以若是再呼叫 getPixels() or setPixels(),可能會得到 exception,所以建議當不會再使用到 bitmap 時再使用。
LruCache
- API level 12 (3.1)
- Support Library back to API level 4 (1.6).
- 管理與 cache 物件,呼叫 get(key) 取出物件,若物件不存在則會呼叫 create() 產生物件,並將新產生的物件加入 head of cache 再將其回傳。
- 加入新物件時,cache 會檢查是否超過指定的大小,若超過則會刪除最後一筆 (tail of cache list)。
// override 這個 method 可以讓我們決定 cache 中每個物件 size 怎麼計算,default returns 1。
@Override
protected int sizeOf( String key, Bitmap value )
{
return value.getByteCount();
}
Note:
Bitmap#getByteCount()
- Supported from API Level 12。
// 建構子中則傳入想預設此 cache 的大小,這裡設定最大為 5 M。
// 所以一旦超過 5 M,物件會從最後一直被移除直到總大小小於 5 M。
public LruMemoryCache(Context context)
{
super( 5 * 1024 * 1024 );
this.context = context;
}
為避免發生 OutOfMemoryError,在系統呼叫 onLowMemory() 時建議呼叫 LruCache#evictAll() 移除所有元素再重建。
Note:
- 但 onLowMemory() 會被呼叫是因為整個系統的空間不足而不是因為你的 App 所用的空間已不足!
- 所以等到此 method 被呼叫時已來不及,更好的方式是在 App 執行一開始便設定適當的大小限制給它。
- 從 ActivityManager#getMemoryClass() (API Level 5) 可以取得該 application 在 該 device 中的分配空間:
- 單位為 megabytes。
- 預設為 16 M,空間大點裝置可到 24 或更高。
也可以利用 LruCache 來管理在 SD card 中的檔案大小:
- 儲存 File object 在 LruCache(override create())。
- 以 file.length() 計算檔案大小 (override sizeOf())。
- 移除物件時刪除 SD card 中的檔案 (override entryRemoved())。
* Reference
- Understanding Weak References | Java.net
- Styling Android » Blog Archive » Memory Cache – Part 1
- Styling Android » Blog Archive » Memory Cache – Part 2
- Styling Android » Blog Archive » Memory Cache – Part 3
- Styling Android » Blog Archive » Memory Cache – Part 4
- WeakHashMap的神话 - - ITeye技术网站
- WeakHashMap是如何清除不用的key的
2012年8月27日 星期一
[Java] Serialize
- 以字串型態傳遞物件。
- 不能跨平台。
- 像物件的 version control id。
- 當物件屬性改變此 id 就會改變。
- 當 B side 接收到 Serialize id,會根據此 id 去 new 相對應的物件將值設定進去。
2012年8月25日 星期六
[ORM] Date and Timestamp
DB 中時間的 type 有:
對應到 java 中的 type 皆是宣告為 Date
DB 都是收到相同的值 然後再依欄位 type 決定存入的值
例如若是 date 會被 truncate 掉 millisecond 後再存入
- Date
- Timestamp
對應到 java 中的 type 皆是宣告為 Date
DB 都是收到相同的值 然後再依欄位 type 決定存入的值
例如若是 date 會被 truncate 掉 millisecond 後再存入
2012年6月15日 星期五
[Database] BigDecimal
當 table 對應為物件時
若其 value 長度可能會超過 java int
這時候就會使用 BigDecimal type
ex: 流水號
* Reference
- How big is BigDecimal?
若其 value 長度可能會超過 java int
這時候就會使用 BigDecimal type
ex: 流水號
* Reference
- How big is BigDecimal?
2012年4月28日 星期六
[SQL] AND has more priority than OR
* Priority
- In SQL: AND > OR
- In Java: && > ||
SELECT count(*) FROM TABLE_A
WHERE (X= 2 OR X = 5) AND Y = 0;
SELECT count(*) FROM TABLE_A
WHERE X IN (2,5) AND Y = 0;
2012年3月25日 星期日
2012年2月16日 星期四
[Web] iframe, location,
iframe
在 iframe 中 submit 的 url,便是載入 iframe 的 url
供 iframe 呼叫的 function 必須寫在 $(function() {}); 之外
window.location
這樣 request method 是 GET,user 只要打網址就可以執行此動作
$().click(function() { }):
Only set click listener not really do the function in click().
$(function() {} )
[jQuery] ready() vs. onload()
在 DOM 下載完後才執行,以避免 function 使用到尚未載入的 element。
AJAX
適合使用 AJAX 的時機,當想保留當下操作頁面的資訊與狀態。
在 iframe 中 submit 的 url,便是載入 iframe 的 url
供 iframe 呼叫的 function 必須寫在 $(function() {}); 之外
window.location
這樣 request method 是 GET,user 只要打網址就可以執行此動作
$().click(function() { }):
Only set click listener not really do the function in click().
$(function() {} )
[jQuery] ready() vs. onload()
在 DOM 下載完後才執行,以避免 function 使用到尚未載入的 element。
AJAX
適合使用 AJAX 的時機,當想保留當下操作頁面的資訊與狀態。
2012年1月29日 星期日
[Java] Memory
Java Heap
Native Memory
Process Size
* Reference
- 何謂Java heap, Native memory and Process Size
- JVM 用來配置 Java Object 的記憶體。
- 大小是透過命令執行列中的參數 -Xmx 所設定。
Native Memory
- JVM 內部運作時所用的空間。
- 使用的空間大小取決於產生的程式碼、Thread、GC 時用於保存 java object 資訊與產生/最佳化程式碼的暫存空間。
Process Size
- 是 Java Heap、Native Memory、被用於已載入執行和函式庫的總合。
* Reference
- 何謂Java heap, Native memory and Process Size
2011年11月16日 星期三
[Java] Object Initialization - (2)
Initialization and inheritance
A note on the word "inherit"
Initializing fields in superclasses
Order of initialization
The structure of <init>
(Almost) every constructor's first act and Automatic invocation of super() (For every class except Object)
Invoking super() with arguments
Only one constructor invocation allowed
Catching exceptions not allowed
When you instantiate a new Child object with the new operator, the JVM will
Steps for new operator
Summary
* Reference
- Object Initialization in Java ***
- When an object is initialized, all the instance variables defined in the object's class must be set to proper initial values.
- When an object is created, the Java virtual machine allocates enough space for all the object's instance variables, which include all fields defined in the object's class and in all its superclasses.
- The data that must be allocated on the heap
- The actual manner of representing objects on the heap is an implementation detail of each particular Java virtual machine.
- Object will have no fields in any given Java platform implementation.
- Because Object is the superclass of all other objects, any fields declared in Object must be allocated for every object used by every Java program.
A note on the word "inherit"
- A class's members are the fields and methods that
- Actually declared in the class
- Inherits from superclasses.
- Only inherits accessible members of its superclasses. (the subclass doesn't override or hide those accessible members.)
Initializing fields in superclasses
- Unlike methods, constructors are never inherited.
- If you don't explicitly declare a constructor in a class
- Will not inherit a constructor from its direct superclass.
- The compiler will generate a default constructor.
- This is because a superclass constructor can't initialize fields in the subclass. A subclass must have its own constructor to initialize its own instance variables.
- => Every class has at least one
method responsible for initializing the class variables explicitly declared in that class. - To fully initialize an object, the Java virtual machine must invoke (at least) one instance initialization method from each class along the object's inheritance path.
Order of initialization
- The fields of an object are initialized
- Starting with the fields declared in the base class.
- Ending with the fields declared in the object's class.
- The order of initialization of fields would be:
- Object's fields (this will be quick, because there are none)
- ParentClass's fields
- ChildClass's fields
- This base-class-first order
- Aims to prevent fields from being used before they are initialized to their proper (not default) values.
- In a child constructor or initializer
- You can safely use a superclass's field directly
- Call a method that uses a superclass's field.
- By the time the code in your child constructor or initializer is executed
- You can be certain that the fields declared in any superclasses have already been properly initialized.
The structure of <init>
- How does Java ensure the correct ordering of initialization?
- The Java compiler generates the instance initialization method.
- Into each <init> method, the compiler can place three kinds of code:
- An invocation of another constructor
- Instance variable initializers
- The constructor body
- The order in which the compiler places these components into the
method determines the order of initialization of an object's fields.
- The first thing each
<init>method will do is invoke another constructor. - The first statement in a constructor
If is this()- The corresponding
<init>method will start by calling another<init>method of the same class. - If not the this()
- The
method for that constructor will begin with an invocation of a superclass constructor. - super()
- You can explicitly invoke a superclass constructor using the super() statement.
- If you don't, the compiler will automatically generate an invocation of the superclass's no-arg constructor.
- This is true for default constructors as well.
- With the exception of class Object, the
method for any default constructor will do only one thing: invoke the method for the superclass's no-arg constructor.
Invoking super() with arguments
- If you want to invoke a superclass constructor that takes parameters,
- You must provide an explicit super() invocation.
- If parent class explicitly declares a constructor,
- The Java compiler won't generate a default constructor.
- If a subclass's direct superclass does not offer a no-arg constructor,
- Eevery constructor in that subclass must begin with either an explicit super() or this()invocation.
Only one constructor invocation allowed
- You can't have both this() and super() in the same constructor.
- Can only have one or the other (or neither, if the direct superclass includes a no-arg constructor).
- If a constructor includes a this() or super() invocation, it must be the first statement in the constructor.
Catching exceptions not allowed
- One other rule enforced on constructors is that you can't catch any exceptions thrown by the constructor invoked with this() or super().
- To do so, you would have to begin your constructor with a try statement.
- If any instance initialization method completes abruptly by throwing an exception, initialization of the object fails.
- This in turn means that object creation fails, because in Java programs, objects must be properly initialized before they are used.
When you instantiate a new Child object with the new operator, the JVM will
- Allocate (at least) enough space on the heap to hold all the instance variables declared in Child object and its superclasses.
- Initialize all the instance variables to their default initial values.
- Invoke the
method in the Child class.
this() won't change the order of initialization- The code for constructor invocations and constructor bodies, the Java compiler also places code for any initializers in the
<init>method. - If a class includes initializers, the code for them will be placed after the superclass method invocation but before the code for the constructor body, in every
<init>method that begins with an explicit or implicitsuper()invocation. - Code for initializers are not included as part of <init> methods that begin with a this() invocation.
- The initializers for a class are guaranteed to be run only once for each new class creation.
Steps for new operator
- 分配空間並且設為 default value。
- Child 會先呼叫其 Parent。
- Parent 會呼叫 Object。
- Object 不會做啥事便回傳。
- Parent 初始欄位為你所設定的初始值、constructor,回傳。
- Note:
- If <init> in a superclass invokes a method that has been overridden in a subclass, the subclass's implementation of that method will run.
- If the subclass's method implementation uses instance variables explicitly declared in the subclass, those variables will still have their default initial values.
- 如果在步驟中呼叫了定義在 child 中的 fields,則會取到它的 default value,因為 Child 並還未經過 <init>
- Child 初始欄位為你所設定的初始值、constructor。
- JVM 回傳 Child object reference。
Summary
- 在 java 中有個 implicit 初始動作: initializers,其 method name is <init>,負責執行你所給的預設值 (="initial value")。
- 初始化的動作是往上優先的(會一直跑到 class Object),然後再依 top-down 的順序,這樣可以確保當 child object 使用到 parent object 時,fields 已經被初始化完成。
- 因此有繼承關係時,一定會呼叫 super(),不論你是否有寫,super() 也必寫在第一行。
- class Object 中沒有任何 fields,因為 Object 是每個 object 的 superclass。
- <init> 的執行時間會在 superclass constructor 後,以及 child class constructor body 前。
- If B extends A
- allocate spaces and set default values for A and B -> call super() in B -> execute A's <init> -> execute A's constructor -> execute B's <init> -> execute B's constructor -> return B's reference to new operator.
- 對 A 而言,B 裡 fields 都是不可視的,即使是 B 覆寫了 A's member。
* Reference
- Object Initialization in Java ***
[Java] Object Initialization - (1)
An object is a chunk of memory bundled with the code that manipulates memory.
* The Java language has three mechanisms dedicated to ensuring proper initialization of objects:
(Instance initializers and instance variable initializers collectively are called "initializers.")
Note
If you explicitly initialize innerCoffee, say to a value of 100, then when each CoffeeCup object is created, innerCoffee will, in effect, be initialized twice.
* The difference between methods and constructors:
* Default constructors
* Instance initialization methods
If you don't explicitly declare a constructor in a class, the Java compiler will create a default constructor on the fly, then translate that default constructor into a corresponding instance initialization method. Thus, every class will have at least one instance initialization method.
* Initializers
* Instance initializers
Java 1.1 introduced the instance initializer, which is also called the instance initialization block.
Instance initializers are a useful alternative to instance variable initializers whenever:
(1) initializer code must catch exceptions, or
(2) perform fancy calculations that can't be expressed with an instance variable initializer.
Advantages
* Initializers can't make forward references
參考物件的順序是無法顛倒,得依照宣告的先後,因此使用了還未宣告到的變數是不可行的,會被報錯,如下:
* Reference
- Object Initialization in Java ***
* The Java language has three mechanisms dedicated to ensuring proper initialization of objects:
(Instance initializers and instance variable initializers collectively are called "initializers.")
- Instance initializers (also called instance initialization blocks)
- Instance variable initializers
- Constructors.
class CoffeeCup {
private int innerCoffee = 100;
//...
}
Note
If you explicitly initialize innerCoffee, say to a value of 100, then when each CoffeeCup object is created, innerCoffee will, in effect, be initialized twice.
- innerCoffee will be given its default initial value of zero.
- The zero will be overwritten with the proper initial value of 100.
* The difference between methods and constructors:
- The name of a class is a valid name for its methods.
- Constructors aren't methods by showing that a constructor does not conflict with a method that has the same signature.
* Default constructors
- If you declare a class with no constructors, the compiler will automatically create a default constructor for the class.
- Takes no parameters (it's a no-arg constructor) and has an empty body.
- All classes are guaranteed to have at least one constructor.
- The compiler gives default constructors the same access level as their class.
* Instance initialization methods
- When you compile a class, the Java compiler creates an instance initialization method for each constructor you declare in the source code of the class.
- Although the constructor is not a method, the instance initialization method is.
- It has a name, <init>
- A return type, void,
- A set of parameters that match the parameters of the constructor from which it was generated.
- Not a valid Java method name, so you could not define a method in your source file that accidentally conflicted with an instance initialization method.
Is not a method in the Java language sense of the term, because it has an illegal name. In the compiled, binary Java class file, however, it is a legal method.
* Initializers
- Besides providing constructors, Java offers one other way for you to assign an initial value to instance variables: initializers (instance variable initializers and instance initializers).
- In an instance variable initializer, you have only an equals sign and one expression.
- private int innerCoffee = 355; // "= 355" is an initializer
- The right-hand side of the equals sign in an initializer can be any expression that evaluates to the type of the instance variable.
* Instance initializers
Java 1.1 introduced the instance initializer, which is also called the instance initialization block.
// The following block is an instance initializer
{
innerCoffee = 355;
}
Instance initializers are a useful alternative to instance variable initializers whenever:
(1) initializer code must catch exceptions, or
(2) perform fancy calculations that can't be expressed with an instance variable initializer.
Advantages
- Can just write the code once.
- You could, of course, always write such code in constructors. But in a class that had multiple constructors, you would have to repeat the code in each constructor.
- Will be executed no matter what constructor is used to create the object.
- Useful in anonymous inner classes, which can't declare any constructors at all.
* Initializers can't make forward references
參考物件的順序是無法顛倒,得依照宣告的先後,因此使用了還未宣告到的變數是不可行的,會被報錯,如下:
class VirtualCafe {
private int chairsCount = 4 * tablesCount;
private int tablesCount = 20;
//...
}
但有些寫法,仍能通過,如下:class VirtualCafe {
private int chairsCount = initChairsCount();
private int tablesCount = 20;
private int initChairsCount() {
return tablesCount * 4;
}
//...
}
- Here chairsCount's initializer sneakily invokes a method that uses tablesCount before its initializer has been executed.
- When initChairsCount() calculates tablesCount * 4, tablesCount is still at its default initial value of zero.
- As a result, initChairsCount() returns zero, and chairsCount is initialized to zero.
* Reference
- Object Initialization in Java ***
2011年11月1日 星期二
2011年10月2日 星期日
[JAVA] 序列化
序列化是將 object 轉為 string,好用來傳遞。
Java 中的序列化只會對 object 的 attributes,而不會連 methods 也序列化。
因為 methods 是不帶狀態的,僅是指令,只要 JVM classloader 可以 loader 到那個類,那麼自然可以獲得 methods (?)。
序列化真正要保存的是 object attributes 的值和 object 類型。
In a word, 序列化會保存 object 的以下內容:
* Reference
- Java序列化机制要序列化那些内容 - 丸子 - ITeye技术网站
Java 中的序列化只會對 object 的 attributes,而不會連 methods 也序列化。
因為 methods 是不帶狀態的,僅是指令,只要 JVM classloader 可以 loader 到那個類,那麼自然可以獲得 methods (?)。
序列化真正要保存的是 object attributes 的值和 object 類型。
In a word, 序列化會保存 object 的以下內容:
- object type
- object attributes' type
- object attributes' value
* Reference
- Java序列化机制要序列化那些内容 - 丸子 - ITeye技术网站
[Java] Java Message Service
* Definition
- The Java Message Service (JMS) API,用來訪問消息收發系統,類似於JDBC(Java Database Connectivity)角色,只是是功能是訊息的傳遞。
- A Java Message Oriented Middleware (MOM) API for sending messages between two or more clients.
- A messaging standard that allows application components based on the Java Enterprise Edition (JEE) to create, send, receive, and read messages.
- 是由Sun與MOM廠商所共同制定的介面,定義了訊息的傳送、接收、頻道(Channel)、主題(Topic)、佇列(Queue)等標準介面。
- 實作部份由廠商完成,Java開發人員只要學習標準API介面的使用,就可以利用各廠商的JMS支援系統來進行訊息傳送、接收等處理(您可以想像JDBC與資料庫廠商之間的關係)。
- ex: 包括 IBM 的 MQSeries、BEA的 Weblogic JMS service和 Progress 的 SonicMQ。
* Advantages
Allows the communication between different components of a distributed application to be loosely coupled, reliable, and asynchronous.
* API
- 頻道是用Destination這個介面來定義。
- 在訊息(Message)觀念中提及,訊息服務有兩種模式:
- 出版-訂閱(Publish-Subscribe)
- 點對點(Point-to-Point)
- 頻道分為:
- 主題
- 佇列
- Destination有兩個子介面Topic與Queue來分別代表。
- 根據兩種模式的不同,ConnectionFactory有TopicConnectionFactory、QueueConnectionFactory兩個子介面。
- 對於訊息產生者、訊息消費者的定義則分別為:
- MessageProducer
- MessageConsumer介面
- 而在兩種模式下,分別有發佈 者(Publisher)、傳送者(Sender),以及訂閱者(Subscriber)、接收者(Receiver)。
* Usage
伺服端必須設定好ConnectionFactory以及Destination,並分別使用一個名稱向JNDI註冊,端點必須使用JNDI名稱查找ConnectionFactory及Destination,JMS端點取得ConnectionFactory,使用其與伺服端建立連線,連線以Connection介面定義。
[Message]
- Messaging is a form of loosely coupled distributed communication, where in this context the term 'communication' can be understood as an exchange of messages between software components.
- Message-oriented technologies attempt to relax tightly coupled communication (such as TCP network sockets, CORBA or RMI) by the introduction of an intermediary component.
- The advantages of messaging include the:
- Ability to integrate heterogeneous platforms.
- Reduce system bottlenecks, increase scalability.
- Respond more quickly to change.
[Message passing]
- A form of communication used in parallel computing, object-oriented programming, and interprocess communication.
- In this model, processes or objects can send and receive messages (comprising zero or more bytes, complex data structures, or even segments of code) to other processes. By waiting for messages, processes can also synchronize.
- Message passing is the paradigm of communication where messages are sent from a sender to one or more recipients. Forms of messages include (remote) method invocation, signals, and data packets.
* Reference
- Java Message Service - Wikipedia, the free encyclopedia
- Message passing - Wikipedia, the free encyclopedia
- 簡介 Java Message Service
- 訊息(Message)觀念
- JMS_百度百科
2011年9月18日 星期日
[Design] Object relationship
假設 A B C 間的關係如下:
A <-> (many-to-one) <-> B <-> (one-to-many) <-> C
但如果按照此關係,設 List在 B 並不符合實務
因為這樣當我們去找 A 相對的 B,B 便會找出其他我們不須要的 A
所以反過來置放。
還有,實際上可看見 A 和 C 沒有直接的對應關係。
A <-> (many-to-one) <-> B <-> (one-to-many) <-> C
但如果按照此關係,設 List在 B 並不符合實務
因為這樣當我們去找 A 相對的 B,B 便會找出其他我們不須要的 A
所以反過來置放。
還有,實際上可看見 A 和 C 沒有直接的對應關係。
2011年9月12日 星期一
[Java] toString()
- 主要是讓平凡人可以讀到某些有意義的資訊,而這些資訊是有關你的類別的物件時,此時就會複寫掉toString( )。
- 其他程式碼可呼叫你的物件的toString()來取得某些關於你的物件的有用細節。
- 當我們使用Sysout.our.println( 物件 ),其實就是印出物件的toString()的回傳值而已。
- 預設其實是印出16進制的HashCode碼。
* Reference
- toString()
訂閱:
文章 (Atom)