IntentService is a base class for {@link Service}s that handle asynchronous 
 requests (expressed as {@link Intent}s) on demand. Clients send requests 
 through {@link android.content.Context#startService(Intent)} calls; the 
 service is started as needed, handles each Intent in turn using a worker 
 thread, and stops itself when it runs out of work.
private final class ServiceHandler extends Handler {
    public ServiceHandler(Looper looper) {
        super(looper);
    }

    @Override
    public void handleMessage(Message msg) {
        onHandleIntent((Intent)msg.obj);
        stopSelf(msg.arg1);
    }
}

public int onStartCommand(Intent intent, int flags, int startId) {
    onStart(intent, startId);
    return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}

public void onStart(Intent intent, int startId) {
    Message msg = mServiceHandler.obtainMessage();
    msg.arg1 = startId;
    msg.obj = intent;
    mServiceHandler.sendMessage(msg);
}
// 创建一个Java Service
public class MyJavaService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        // 在此处进行初始化操作
        // 注册需要的监听器、初始化需要的资源等等
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 在此处处理Service的逻辑
        // 执行系统级别的任务
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 在此处释放资源
        // 取消注册的监听器、释放占用的资源等等
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        // 不需要绑定操作,返回null即可
        return null;
    }
}
public class Myservice extends Service{
	@Override
	public IBinder onBind(Intent intent){
		return null;
	}
	@Override
	public int onStartCommand(Intent intent,int flags,int startId){
		return super.onStartCommand(intent,flags,startId);
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
}
1. public class Item implements
2. //  所有的属性和getter/setter方法同上,省略 
3. public
4.                         Bid currentMaxBid, Bid currentMinBid)   
5. throws
6.        
7. // Check highest bid (can also be a different Strategy (pattern)) 
8. if (currentMaxBid != null && currentMaxBid.getAmount().compareTo(bidAmount) > 0) {   
9. throw new BusinessException("Bid too low.");   
10.         }   
11.        
12. // Auction is active 
13. if
14. throw new BusinessException("Auction is not active yet.");   
15.        
16. // Auction still valid 
17. if ( this.getEndDate().before( new
18. throw new BusinessException("Can't place new bid, auction already ended.");   
19.        
20. // Create new Bid 
21. new Bid(bidAmount, this, bidder);   
22.        
23. // Place bid for this Item 
24. this.getBids.add(newBid);  // 请注意这一句,透明的进行了持久化,但是不能在这里调用ItemDao,Item不能对ItemDao产生依赖! 
25.        
26. return
27.     }   
28. }
public void updateCustomerDeviceAndInstallInfo(long customerId, String channelKey,
                   String androidId, String imei, String gaId,
                   String gcmPushToken, String instanceId) {}
#修改主机名
hostnamectl set-hostname node2
hostnamectl set-hostname node3
public class PlayMusicService extends Activity implements OnClickListener {

private Button playBtn;
private Button stopBtn;
private Button pauseBtn;
private Button exitBtn;
private Button closeBtn;

private Intent intent;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_service);

playBtn = (Button) findViewById(R.id.play);
stopBtn = (Button) findViewById(R.id.stop);
pauseBtn = (Button) findViewById(R.id.pause);
exitBtn = (Button) findViewById(R.id.exit);
closeBtn = (Button) findViewById(R.id.close);

playBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
exitBtn.setOnClickListener(this);
closeBtn.setOnClickListener(this);

}

@Override
public void onClick(View v) {
int op = -1;
intent = new Intent("com.homer.service.musicService");

switch (v.getId()) {
case R.id.play: // play music
op = 1;
break;
case R.id.stop: // stop music
op = 2;
break;
case R.id.pause: // pause music
op = 3;
break;
case R.id.close: // close activity
this.finish();
break;
case R.id.exit: // stopService
op = 4;
stopService(intent);
this.finish();
break;
}

Bundle bundle = new Bundle();
bundle.putInt("op", op);
intent.putExtras(bundle);

startService(intent); // startService
}

@Override
public void onDestroy(){
super.onDestroy();

if(intent != null){
stopService(intent);
}
}
}
public class PlayMusicService extends Activity implements OnClickListener {

private Button playBtn;
private Button stopBtn;
private Button pauseBtn;
private Button exitBtn;
private Button closeBtn;

private Intent intent;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_service);

playBtn = (Button) findViewById(R.id.play);
stopBtn = (Button) findViewById(R.id.stop);
pauseBtn = (Button) findViewById(R.id.pause);
exitBtn = (Button) findViewById(R.id.exit);
closeBtn = (Button) findViewById(R.id.close);

playBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
exitBtn.setOnClickListener(this);
closeBtn.setOnClickListener(this);

}

@Override
public void onClick(View v) {
int op = -1;
intent = new Intent("com.homer.service.musicService");

switch (v.getId()) {
case R.id.play: // play music
op = 1;
break;
case R.id.stop: // stop music
op = 2;
break;
case R.id.pause: // pause music
op = 3;
break;
case R.id.close: // close activity
this.finish();
break;
case R.id.exit: // stopService
op = 4;
stopService(intent);
this.finish();
break;
}

Bundle bundle = new Bundle();
bundle.putInt("op", op);
intent.putExtras(bundle);

startService(intent); // startService
}

@Override
public void onDestroy(){
super.onDestroy();

if(intent != null){
stopService(intent);
}
}
}
public class PlayMusicService extends Activity implements OnClickListener {

private Button playBtn;
private Button stopBtn;
private Button pauseBtn;
private Button exitBtn;
private Button closeBtn;

private Intent intent;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_service);

playBtn = (Button) findViewById(R.id.play);
stopBtn = (Button) findViewById(R.id.stop);
pauseBtn = (Button) findViewById(R.id.pause);
exitBtn = (Button) findViewById(R.id.exit);
closeBtn = (Button) findViewById(R.id.close);

playBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
exitBtn.setOnClickListener(this);
closeBtn.setOnClickListener(this);

}

@Override
public void onClick(View v) {
int op = -1;
intent = new Intent("com.homer.service.musicService");

switch (v.getId()) {
case R.id.play: // play music
op = 1;
break;
case R.id.stop: // stop music
op = 2;
break;
case R.id.pause: // pause music
op = 3;
break;
case R.id.close: // close activity
this.finish();
break;
case R.id.exit: // stopService
op = 4;
stopService(intent);
this.finish();
break;
}

Bundle bundle = new Bundle();
bundle.putInt("op", op);
intent.putExtras(bundle);

startService(intent); // startService
}

@Override
public void onDestroy(){
super.onDestroy();

if(intent != null){
stopService(intent);
}
}
}
apiVersion: apps/v1    # #与k8s集群版本有关,使用 kubectl api-versions 即可查看当前集群支持的版本
kind: Deployment       # 资源类型为Deployment
metadata:          # 元数据,即 Deployment 的一些基本属性和信息
  name: hello-deployment  # Deployment 的名称
  labels:              # 标签,可以灵活定位一个或多个资源,其中key和value均可自定义,可以定义多组
    app: hello         # 为该Deployment设置key为app,value为hello的标签
spec:              # 这是关于该Deployment的描述          
  replicas: 3          # 使用该Deployment创建3个应用程序实例
  selector:            # 标签选择器
    matchLabels:       # 选择包含标签app:hello的资源 
      app: hello
  template:            # 这是选择或创建的Pod的模板
    metadata:            # Pod的元数据
      name: hello        # Pod的名称,K8s会在后面加上字符区分不同的Pod
      labels:            # Pod的标签,上面的selector即选择包含标签app:hello的Pod
        app: hello
    spec:              # 关于该Pod的描述    
      containers:        # container,与docker中的container是同一种
      - name: tomcat     # container的名称
        image: huangxjie/tomcat:0.0.1  # 使用我的测试镜像
        ports:
        - containerPort: 8080  # 容器访问端口
static_configs: 静态服务发现
dns_sd_configs: DNS 服务发现
file_sd_configs: 文件服务发现
consul_sd_configs: Consul 服务发现
serverset_sd_configs: Serverset 服务发现
nerve_sd_configs: Nerve 服务发现
marathon_sd_configs: Marathon 服务发现
kubernetes_sd_configs: Kubernetes 服务发现
gce_sd_configs: GCE 服务发现
ec2_sd_configs: EC2 服务发现
openstack_sd_configs: OpenStack 服务发现
azure_sd_configs: Azure 服务发现
triton_sd_configs: Triton 服务发现
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.integrated.ac.entity.ActivityInfo;
import com.integrated.ac.mapper.ActivityInfoMapper;
import com.integrated.ac.service.ActivityInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.integrated.dt.entity.Question;
import com.integrated.dt.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yan
 * @since 2021-10-05
 */
@Service
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo> implements ActivityInfoService {

    @Autowired
    private QuestionService questionService;

    @Override
    public void loadActivityQuestion() {
        List<ActivityInfo> activityInfoList = this.list(new QueryWrapper<>(new ActivityInfo().setIisEffect(1)));
        if (CollectionUtil.isNotEmpty(activityInfoList)) {
            activityInfoList.forEach(activityInfo ->
                    questionService.loadActivityQuestion(activityInfo.getId()));

        }
    }
}
<span style="font-size:18px;">public class HelloService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;

// 处理从线程收到的消息们
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// 通常我们在这里做一些工作比如下载一个文件
// 在我们的例子中,仅仅是睡5秒钟.
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
// 使用startId停止服务,从而使我们不会在处理
// 另一个工作的中间停止service
stopSelf(msg.arg1);
}
}

@Override
public void onCreate() {
// 启动运行service的线程.注意我创建了一个
// 分离的线程,因为service通常都是在进程的
// 主线程中运行,但我们不想让主线程阻塞.我们还把新线程
// 搞成后台级的优先级,从而减少对UI线程(主线程的影响).
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();

// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

// 对于每个开始请求,发送一消息来开始一次工作,并且把
// start ID也传过去,所以当完成一个工作时,我们才知道要停止哪个请求.
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);

// 如果我们在这里返回后被被杀死了,重启之.
return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}

@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
}
</span>
@Service("baseCacheService")
public class BaseCacheServiceImpl implements BaseCacheService{
}

@Service 
public class ShortUrlServiceImpl implements ShortUrlService {
}
package com.example.lt.boundservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

public class LocalService extends Service{

    private String[] names = {"吕布","赵子龙","关羽"};

    private IBinder myBinder = new MyBinder();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    public class MyBinder extends Binder{
        public LocalService getService(){
            return LocalService.this;
        }
    }

    public String getName(int postion){
        return names[postion];
    }
}
Interface Servlet -> abstract GenericServlet -> abstract HttpServlet
 
   1.init() 
  
 
   2.getServletConfig() 
  
 
   3.service() 
  
 
   4.getServletInfo() 
  
 
   5.destroy()
  • 1
  • 2
  • 3
  • 4
  • 5