Skip to content

Commit

Permalink
增加netty例子
Browse files Browse the repository at this point in the history
  • Loading branch information
TedYim committed Jan 10, 2019
1 parent 5e1b611 commit 572ac74
Show file tree
Hide file tree
Showing 45 changed files with 1,499 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@
<version>4.1.4</version>
</dependency>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.6.Final</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/com/example/demo/netty/ch10/FastThreadLocalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.demo.netty.ch10;

import io.netty.util.concurrent.FastThreadLocal;

public class FastThreadLocalTest {
private static FastThreadLocal<Object> threadLocal0 = new FastThreadLocal<Object>() {
@Override
protected Object initialValue() {
return new Object();
}

@Override
protected void onRemoval(Object value) throws Exception {
System.out.println("onRemoval");
}
};

private static FastThreadLocal<Object> threadLocal1 = new FastThreadLocal<Object>() {
@Override
protected Object initialValue() {
return new Object();
}
};


public static void main(String[] args) {
new Thread(() -> {
Object object = threadLocal0.get();
// .... do with object
System.out.println(object);
threadLocal0.set(new Object());

// while (true) {
// threadLocal0.set(new Object());
// try {
// Thread.sleep(1);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}).start();

new Thread(() -> {
Object object = threadLocal0.get();
// ... do with object
System.out.println(object);
while (true) {
System.out.println(threadLocal0.get() == object);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/example/demo/netty/ch10/RecycleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.demo.netty.ch10;

import io.netty.util.Recycler;

public class RecycleTest {
private static final Recycler<User> RECYCLER = new Recycler<User>() {
@Override
protected User newObject(Handle<User> handle) {
return new User(handle);
}
};

private static class User {
private final Recycler.Handle<User> handle;

public User(Recycler.Handle<User> handle) {
this.handle = handle;
}

public void recycle() {
handle.recycle(this);
}
}

public static void main(String[] args) {
User user = RECYCLER.get();

user.recycle();
RECYCLER.get().recycle();

User user1 = RECYCLER.get();

System.out.println(user1 == user);
}
}
72 changes: 72 additions & 0 deletions src/main/java/com/example/demo/netty/ch11/Decorate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.example.demo.netty.ch11;

/**
* @see io.netty.buffer.WrappedByteBuf;
* @see io.netty.buffer.UnreleasableByteBuf
* @see io.netty.buffer.SimpleLeakAwareByteBuf
*/
public class Decorate {

// 优惠方案
public interface OnSalePlan {
float getPrice(float oldPrice);
}

// 无优惠
public static class NonePlan implements OnSalePlan {
static final OnSalePlan INSTANCE = new NonePlan();

private NonePlan() {

}

public float getPrice(float oldPrice) {
return oldPrice;
}
}

// 立减优惠
public static class KnockPlan implements OnSalePlan {
// 立减金额
private float amount;

public KnockPlan(float amount) {
this.amount = amount;
}

public float getPrice(float oldPrice) {
return oldPrice - amount;
}
}


// 打折优惠
public static class DiscountPlan implements OnSalePlan {
// 折扣
public int discount;
private OnSalePlan previousPlan;

public DiscountPlan(int discount, OnSalePlan previousPlan) {
this.discount = discount;
this.previousPlan = previousPlan;
}

public DiscountPlan(int discount) {
this(discount, NonePlan.INSTANCE);
}

public float getPrice(float oldPrice) {
return previousPlan.getPrice(oldPrice) * discount / 10;
}
}

public static void main(String[] args) {
DiscountPlan simpleDiscountPlan = new DiscountPlan(5);
System.out.println(simpleDiscountPlan.getPrice(100));

KnockPlan previousPlan = new KnockPlan(50);
DiscountPlan complexDiscountPlan = new DiscountPlan(5, previousPlan);
System.out.println(complexDiscountPlan.getPrice(100));
}

}
31 changes: 31 additions & 0 deletions src/main/java/com/example/demo/netty/ch11/IterableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.demo.netty.ch11;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;

public class IterableTest {

public static void main(String[] args) {
ByteBuf header = Unpooled.wrappedBuffer(new byte[]{1, 2, 3});
ByteBuf body = Unpooled.wrappedBuffer(new byte[]{4, 5, 6});

ByteBuf merge = merge(header, body);
merge.forEachByte(value -> {
System.out.println(value);
return true;
});


}

public static ByteBuf merge(ByteBuf header, ByteBuf body) {
CompositeByteBuf byteBuf = ByteBufAllocator.DEFAULT.compositeBuffer(2);
byteBuf.addComponent(true, header);
byteBuf.addComponent(true, body);

return byteBuf;
}

}
147 changes: 147 additions & 0 deletions src/main/java/com/example/demo/netty/ch11/ObserverTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.example.demo.netty.ch11;

import io.netty.channel.ChannelFuture;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.util.ArrayList;
import java.util.List;

public class ObserverTest {
/**
* 被观察者
*/
public interface Observerable {
void registerObserver(Observer o);

void removeObserver(Observer o);

void notifyObserver();
}

/**
* 观察者
*/
public interface Observer {
void notify(String message);
}

public static class Girl implements Observerable {
private String message;

List<Observer> observerList;

public Girl() {
observerList = new ArrayList<>();
}


@Override
public void registerObserver(Observer o) {
observerList.add(o);
}

@Override
public void removeObserver(Observer o) {
observerList.remove(o);
}

@Override
public void notifyObserver() {
for (Observer observer : observerList) {
observer.notify(message);
}
}

public void hasBoyFriend() {
message = "女神有男朋友了";
notifyObserver();
}

public void getMarried() {
message = "女神结婚了,你们都死心吧!";
notifyObserver();
}

public void getSingled() {
message = "女神单身了,你们有机会了!";
notifyObserver();
}
}

/**
* 男孩
*/
public static class Boy implements Observer {
public void notify(String message) {
System.out.println("男孩收到消息:" + message);
}
}

/**
* 男人
*/
public static class Man implements Observer {
public void notify(String message) {
System.out.println("男人收到消息:" + message);
}
}

/**
* 老男人
*/
public static class OldMan implements Observer {
public void notify(String message) {
System.out.println("老男人收到消息:" + message);
}
}

public static void main(String[] args) {
Girl girl = new Girl();
Boy boy = new Boy();
Man man = new Man();
OldMan oldMan = new OldMan();

// 通知男孩、男人、老男人,女神有男朋友了
girl.registerObserver(boy);
girl.registerObserver(man);
girl.registerObserver(oldMan);
girl.hasBoyFriend();
System.out.println("====================");

// 通知男孩,男人,女神结婚了
girl.removeObserver(oldMan);
girl.getMarried();
System.out.println("====================");


girl.registerObserver(oldMan);
girl.getSingled();
}


public void write(NioSocketChannel channel, Object object) {
ChannelFuture channelFuture = channel.writeAndFlush(object);
channelFuture.addListener(future -> {
if (future.isSuccess()) {

} else {

}
});
channelFuture.addListener(future -> {
if (future.isSuccess()) {

} else {

}
});
channelFuture.addListener(future -> {
if (future.isSuccess()) {

} else {

}
});
}

}
28 changes: 28 additions & 0 deletions src/main/java/com/example/demo/netty/ch11/Singleton.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.demo.netty.ch11;

import io.netty.handler.codec.mqtt.MqttEncoder;
import io.netty.handler.timeout.ReadTimeoutException;

/**
* @see ReadTimeoutException
* @see MqttEncoder
*/


public class Singleton {
private static Singleton singleton;

private Singleton() {
}

public static Singleton getInstance() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
Loading

0 comments on commit 572ac74

Please sign in to comment.